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

Dar*_*mov 6 c# process

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

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());
      }
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Jon*_*eet 14

我怀疑的问题是,你指定的文件名是相对于你的工作目录,以及你希望Process.Start寻找有启动进程时-我不相信,当它的工作原理是这样UseShellExecutefalse.尝试只指定要启动的进程的绝对文件名:

process.StartInfo.FileName = @"C:\Projects\MyProgram.exe";
Run Code Online (Sandbox Code Playgroud)

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


f4r*_*4r4 5

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

EG:代替:

C:\ Windows \ System32 \ whoiscl.exe

它应该是:

C:\ Windows \ Sysnative \ whoiscl.exe

希望这可以帮助某人