本范例演示了C#中如何在不打开dos窗口的情况下运行控制台程序,同时捕获程序的输出信息
// This snippet needs the "System.Diagnostics" // library // Application path and command line arguments string ApplicationPath = "C:\\example.exe"; string ApplicationArguments = "-c -x"; // Create a new process object Process ProcessObj = new Process(); // StartInfo contains the startup information of // the new process ProcessObj.StartInfo.FileName = ApplicationPath; ProcessObj.StartInfo.Arguments = ApplicationArguments; // These two optional flags ensure that no DOS window // appears ProcessObj.StartInfo.UseShellExecute = false; ProcessObj.StartInfo.CreateNoWindow = true; // If this option is set the DOS window appears again :-/ // ProcessObj.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // This ensures that you get the output from the DOS application ProcessObj.StartInfo.RedirectStandardOutput = true; // Start the process ProcessObj.Start(); // Wait that the process exits ProcessObj.WaitForExit(); // Now read the output of the DOS application string Result = ProcessObj.StandardOutput.ReadToEnd();