In this post we will have a look on how to create a run application using c sharp just like the windows os Run, well not exactly but just a replica of it.
Design Phase: Here’s the view at the final output so what I want you to do is create a windows based project and drag and drop tools in order to make the view look like the one in the given figure
After creating a view just like the above I want you to double click the Ok button and generate the click event, but before we write the code I want you to import a namespace first and the name of the namespace is
using System.Diagnostics;
System.Diagnostics: This name space is used to give you the data associated with a particular process such as the machine name on which the process is running, exit time, events etc (refer this link for more detail explanation).
This namespace has a class called as the Process which is used to start and stop any local system process by just entering the process name with the help of its function called Start.
Now moving back to our code what we did was clicked the button on our form to generate its click event now look at the code below and add the following code inside your button click as well so here’s how it looks
Code Explanation: From the above explanation of the namespace I said it has a class called as Process and it has a method called as start which will accept the process name as string and start the process typed by the user and on the hit of the Ok button and the said process will be executed.
On the Hit of the cancel button the application will just clear the text field that's all nothing much.
In the below pic i have given the output for this application by typing winword which is a command to open the Microsoft office.
and now after pressing the Ok button the Microsoft word app will be triggered and in case your system does not have word installed then in that case i would suggest using a try catch block to handle the exception thrown by the Process class.
i hope this post was helpful to you in some way or the other, do comment if you like positive or negative comments are always welcome as they help me to help you.
No comments:
Post a Comment