“系统找不到指定的文件”错误on process.Start();

我试图让进程响应为一个字符串,所以我可以在我的代码中的不同位置使用它,这是我到目前为止的解决方案:

const string ex1 = @"C:\Projects\MyProgram.exe "; const string ex2 = @"C:\Projects\ProgramXmlConfig.xml"; Process process = new Process(); process.StartInfo.WorkingDirectory = @"C:\Projects"; process.StartInfo.FileName = "MyProgram.exe "; process.StartInfo.Arguments = ex2; process.StartInfo.Password = new System.Security.SecureString(); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; try { process.Start(); StreamReader reader = process.StandardOutput; string output = reader.ReadToEnd(); } catch (Exception exception) { AddComment(exception.ToString()); } 

但是当我跑步时,我得到:

 "The system cannot find the file specified" error in process.Start(); without process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; 

代码运行正常,但它只是打开控制台窗口,所有进程响应都在那里,因此我不能将它用作字符串。

有谁知道为什么我得到这个错误或者可能是我的问题的不同解决方案?

我怀疑问题是您指定的文件名是相对于您的工作目录的,并且您希望Process.Start在启动进程时查看它 – 当UseShellExecutefalse时,我不相信它会这样。 尝试只指定要启动的进程的绝对文件名:

 process.StartInfo.FileName = @"C:\Projects\MyProgram.exe"; 

请注意,我还从为FileName属性分配的字符串末尾删除了空格 – 完全有可能也是这个问题。

对于System32访问,如果您尝试在x64上运行x86应用程序,则必须在文件名中使用“Sysnative”关键字而不是“System32”。

EG:而不是:

C:\ WINDOWS \ SYSTEM32 \ whoiscl.exe

它应该是:

C:\ WINDOWS \ Sysnative \ whoiscl.exe

希望这有助于某人