Tuesday, June 2, 2015

Execute command prompt command through simple application


Hi all,

I think most of developers are familiar with more than one programing languages, But within these different choices you might choose one programming language as the best choice. Today, I’ll introduce my best choice. It’s C# in the other term C sharp. Actually, one of my favorite book series of Head First, clearly mention learning C# is not a learning Microsoft Visual Studio IDE. A lot of students think learning the Visual Studio IDE is more enough to deal with C#. But it is completely wrong. Because Visual Studio IDE provide lots of features in a very user-friendly manner, but the thing is you should know how to use these features in an optimal way. Otherwise, it might be a headache while implementing your program in practical world.

Within this post, I’ll explain step by steps about how to develop an application using Visual Studio, Actually I’ll develop a small application for executing command prompt command, but I’ll decide explain each step because of the beginners.

In C# we have different choices for building an application such as Windows Form, Windows Presentation Foundation (WPF), and Mobile Application Development likewise. For this demonstration I choose Windows Presentation Foundation (WPF). WPF has a lot of customizability because of the XAML interface formatting future, and also WPF support many of new features introduce by Microsoft with every new Visual Studio version release. Because of these kind of advantages my choice is always WPF.

Ok, let’s go through the demonstration. For this demo I used Visual Studio 2012 IDE. But it’s not a latest version. If you are a university student, you can easily get the Visual Studio latest version with lots of new festers through Dream Spark account. If you are using, these kind of genuine copy, you can build your projects with an aid of Microsoft Team Foundation Service, Team foundation is a Microsoft own version control system. Lots of companies in the modern world use this feature, it's also compatible with an Agile development methodology. Using Team Foundation, developers can deal with project sponsors in a very easy manner. 

Before we start this demo, I think most of you know hoe to shout down the PC to the given time, using command prompt. Because of the beginners I’ll explain how to shutdown PC through command prompt. User can set time period as the second for executing the shutdown command, I set 3600s (=1hr). That means my PC will shut down after 1hr.



Ok, I think now you have some idea about my application, I’ll develop an application for executing above command line through WPF application.  

         1. Start new project

          2. Select Project type as WPF/Set Project name as “ShutdownTimerApp”

           3. Here’s the main canvas, top of the canvas you can drag and drop Text Boxes, Labels, etc. from Toolbox.

         4. Design Graphical User Interface
a.      Drag and drop three Textboxes and one Button
       

b.      Click on the top of element and do the changes in properties window or in XAML design.
                                                              i.      Changing the name of the element
                                          


                                                            ii.      Changing the appearance of the element
                                          



                                                          iii.      Changing the contain and text format of the element
                                         

  
          5.Double click on the top of the Button, the IDE will automatically generate the code segment for you. Within the generated method you can define on click method.




using System.Diagnostics; //Add C# directive (C# Reference)

private void Button_Click_1(object sender, RoutedEventArgs e)
{

int hours = 3600*int.Parse(hoursTextBox.Text);
int minutes = 60*int.Parse(minutesTextBox.Text);
int seconds = int.Parse(secondsTextBox.Text);

int time = hours + minutes + seconds;

Process.Start("shutdown", "/s /t " + time);

}



Here’s my simple & smart app developed by using WPF, see how easy and its customizability. Software developing world becomes a fabulous place because of the combination of C# and this amazing Visual Studio IDE.

Happy Coding….

Best Regards, 
Denuwan Himanga.

No comments:

Post a Comment