4

我知道这个问题之前已经被问过,我之前已经尝试过这些帖子中给出的所有解决方案,但我似乎无法让它发挥作用:-

static void CallBatch(string path)
        {
            int ExitCode;
            Process myProcess;

            ProcessStartInfo ProcessInfo;
            ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + path);
            ProcessInfo.CreateNoWindow = true;
            ProcessInfo.UseShellExecute = true;

            myProcess = Process.Start(ProcessInfo);
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcess.WaitForExit();

            myProcess.EnableRaisingEvents = true;
            myProcess.Exited += new EventHandler(process_Exited);

            ExitCode = myProcess.ExitCode;

            Console.WriteLine("ExitCode: " + ExitCode.ToString(), "ExecuteCommand");
            myProcess.Close();
        }

当我尝试调用批处理文件时,即使 createNoWindow 和 UseShellExecute 都设置为 true,它仍然显示窗口。

我应该放点别的东西让它静默运行批处理文件吗?

4

1 回答 1

7

试试这个:

Process myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/c " + path;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(process_Exited);
myProcess.Start();
myProcess.WaitForExit();
ExitCode = myProcess.ExitCode;

myProcess.StartInfo这个想法是在你开始你的过程之后不要操纵:它是无用的。您也不需要设置UseShellExecutetrue,因为您是通过调用自己启动 shell 的cmd.exe

于 2012-09-19T01:49:50.310 回答