Opening the .net Command Prompt Programatically

In a recent forum posting, I found myself writing some code that would open the Visual Studio 2005 Command Prompt (C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat). Once opened, the command prompt had to accept command-lines, just as if they were typed in by the user…except in this case it had to be under program control!

I’m sure that there are many ways of doing this, but here’s what I ended up with:

[code lang=”C#”]
using System.Diagnostics;
using System.IO;

namespace Cmd
public partial class Form1 : Form
public Form1()

private void button1_Click(object sender, EventArgs e)
string sProcess = @”C:\windows\system32\cmd.exe”;
string sParam = @”C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat”;
string cmd = String.Format(” /k {0}{1}{2} x86″, “\””, sParam, “\””);

Process p = new Process();
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;

p.StartInfo.FileName = sProcess;
p.StartInfo.Arguments = cmd;

System.IO.StreamReader sOut = p.StandardOutput;
StreamWriter myStreamWriter = p.StandardInput;

myStreamWriter.WriteLine(“dir”); // Your command line, MSBuild, etc.



I’ve left the call to MessageBox.Show() to make you aware of the output.

Technorati Tags: , , , , , , , , ,