Thursday, May 27, 2010

Start Process With C#

With the .Net Framework it is pretty easy to start an external process / programm.
The command System.Diagnostics.Process.Start() starts the exe file, which is given as argument.
To start notepad for example, enter the following:
System.Diagnostics.Process.Start(System.Environment.SystemDirectory + "\\notepad.exe");

System.Environment.SystemDirectory returns the path to the system directionary (in my case, C:\Windows\system32).
Furthermore, there are two backslashes in front of \\notepad.exe, because "\" has to be "escaped".
A single "\" alone would make the compiler view "\n" as a special character, "\\" tell it, that we really mean an actual "\".
The function Process.Start() can accept also other parameters beneath the path, with which the programm will be called.
If in the above example notepad is to be started with opening the file Beispiel.txt, which sits on my desktop, the command has to be changed as follows:
System.Diagnostics.Process.Start(System.Environment.SystemDirectory + "\\notepad.exe", "C:\\Users\\Oliver\\Desktop\\Beispiel.txt");

No comments:

Post a Comment