2012年9月24日 星期一

c# run a batch file

using System.Diagnostics;

private static void Run(string args)
        {
            Process proc = new Process();

            proc.StartInfo.FileName = args;
            proc.StartInfo.Arguments = "";
            proc.StartInfo.WorkingDirectory = Application.StartupPath;
            // Redirect the output stream of the child process.
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // p.WaitForExit();
            // Read the output stream first and then wait.
            string output = proc.StandardOutput.ReadToEnd();
            Console.WriteLine(output);
            proc.WaitForExit();
            int exitCode = proc.ExitCode;
            Console.WriteLine("Exit code: " + exitCode);
            proc.Close();
        }

沒有留言: