[英]Executing batch file from c# silently
I know this question has been asked previously and I have tried all the solutions given in those posts before but I can't seem to make it work:- 我知道之前已经问过这个问题,我之前尝试过这些帖子中给出的所有解决方案,但我似乎无法使其发挥作用: -
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();
}
When I try to call the batch file, it still shows the window even though createNoWindow and UseShellExecute are both set to true. 当我尝试调用批处理文件时,即使createNoWindow和UseShellExecute都设置为true,它仍会显示窗口。
Should I put something else to make it run the batch file silently? 我应该放一些其他东西让它以静默方式运行批处理文件吗?
Try this: 试试这个:
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;
The idea is to not manipulate myProcess.StartInfo
after you have started your process: it is useless. 我们的想法是在您启动流程后不操作myProcess.StartInfo
:它没用。 Also you do not need to set UseShellExecute
to true
, because you are starting the shell yourself by calling cmd.exe
. 此外,您不需要将UseShellExecute
设置为true
,因为您通过调用cmd.exe
UseShellExecute
启动shell。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.