首页 > 解决方案 > 通过C#和cmd命令执行.exe和参数

问题描述

尽管对我的问题有一些解释,但我无法解决以下 cmd 命令问题。我的目标是通过 C# 启动一个带有一些参数的 .exe,它必须连续操作多个文件。即使我等到过程完成,它也不会结束。不等待结束(Process.WaitForExit()),看起来不同的命令在没有执行的情况下相互终止。如何通过以下方法实现我想要的每个 .exe 文件的执行?

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"\c Path_of_the_exe " + Path_of_the_file;

using (Process exeProcess = Process.Start(startInfo))
{
   exeProcess.WaitForExit();
}

标签: c#

解决方案


我想不出您需要使用 cmd.exe 来启动 exe 的任何原因。您可以直接启动您的 exe:

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = false;
startInfo.FileName = Path_of_the_exe;
startInfo.Arguments = Path_of_the_file;

using (Process exeProcess = Process.Start(startInfo))
{
   exeProcess.WaitForExit();
}

像您一样启动 cmd.exe 可能会解释您遇到的行为


推荐阅读