Heres a neat little snippit that comes in handy for running commands against the operating system.

Sourced directly from here: http://stackoverflow.com/questions/691716/running-cmd-commands-via-net ...but I like having the code here as well.

 
// Kills a process
<span> </span> private static void ExecuteCommand(string command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters. &gt; tell the command to execute the command that follows
System.Diagnostics.ProcessStartInfo procStartUpInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartUpInfo.RedirectStandardOutput = true;
procStartUpInfo.UseShellExecute = false;
// Do not create a window.
procStartUpInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartUpInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
Console.WriteLine(objException);
}
}
 

Technorati Tags: , , , ,