Invoking cmd.exe from .NET
One of the primary reasons I am writing this blog is to give back to the community -- technical blogs and message board archives have saved my bacon many times, so my plan is, whenever I spend an hour or two figuring something out and I wasn't able to find any concise resources about it, I'll write a post on it so that the next person may stumble across it and save some time.
Yesterday I was working on calling cmd.exe from within .NET, and had to piece together a few things from various places. Here is the code I ended up with. I've added comments to explain the reason for each line of code -- hopefully someone will find this helpful.
using System; using System.Diagnostics; namespace Utilities { public class Command { public static string ExecuteCmd(string arguments) { // Create the Process Info object with the overloaded constructor // This takes in two parameters, the program to start and the // command line arguments. // The arguments parm is prefixed with "@" to eliminate the need // to escape special characters (i.e. backslashes) in the // arguments string and has "/C" prior to the command to tell // the process to execute the command quickly without feedback. ProcessStartInfo _info = new ProcessStartInfo("cmd", @"/C " + arguments); // The following commands are needed to redirect the // standard output. This means that it will be redirected // to the Process.StandardOutput StreamReader. _info.RedirectStandardOutput = true; // Set UseShellExecute to false. This tells the process to run // as a child of the invoking program, instead of on its own. // This allows us to intercept and redirect the standard output. _info.UseShellExecute = false; // Set CreateNoWindow to true, to supress the creation of // a new window _info.CreateNoWindow = true; // Create a process, assign its ProcessStartInfo and start it Process _p = new Process(); _p.StartInfo = _info; _p.Start(); // Capture the results in a string string _processResults = _p.StandardOutput.ReadToEnd(); // Close the process to release system resources _p.Close(); // Return the output stream to the caller return _processResults; } } }