using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ProcessTest2(); } static void ProcessTest2() { var p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.EnableRaisingEvents = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.UseShellExecute = false; p.Exited += new EventHandler(p_Exited); p.Start();
// Visual Studio Command Prompt (2010) p.StandardInput.WriteLine(@"%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86"); p.StandardInput.WriteLine("exit"); p.StandardInput.WriteLine("exit"); p.WaitForExit(); } static void ProcessTest1() { var p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.EnableRaisingEvents = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.UseShellExecute = false; p.Exited += new EventHandler(p_Exited); p.Start(); p.StandardInput.WriteLine("del c:\\1.txt"); p.StandardInput.WriteLine("exit"); p.WaitForExit(); } static void p_Exited(object sender, EventArgs e) { using (StreamWriter sw = new StreamWriter("c:\\" + Guid.NewGuid().ToString() + ".txt")) { sw.Write(DateTime.Now); } Console.WriteLine("exit event"); } } }