Saturday, August 23, 2008

How to run applications under system control programatically through C#

This Article describes how to run applications under control of o/s programatically with C#.For example one have to open notepad programatically or Internet Explorer or any other *.EXE application installed in system.
.Net framework provides 'System.Diagnostics' namespace which provides types to interact with process under o/s.For our current requirement we need 'Process' class under 'System.Diagnostics' namespace.'Process' provides static & instance methods to run an *.EXE application.Below i presented code to open notepad programatically.
//code
Process.Start("NOTEPAD");

the above piece of code opens the notepad.
Its also possible to send parameters to Process with the help of 'ProcessStartInfo' class.
For example if one wants to open internetExplorer & navigate to Google home page programatically , it can be done with following piece of code.

//code

ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "IEXPLORE";
startinfo.Arguments = "Http://google.com";
Process.Start(startinfo);

When you run above code , a new IE window will be opened with Google home page.

No comments: