“系统找不到指定的文件"process.Start() 上的错误; [英] "The system cannot find the file specified" error on process.Start();

查看:321
本文介绍了“系统找不到指定的文件"process.Start() 上的错误;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让进程以字符串形式响应,以便我可以在代码中的不同位置使用它,这是我目前的解决方案:

const string ex1 = @"C:\Projects\MyProgram.exe ";const string ex2 = @"C:\Projects\ProgramXmlConfig.xml";进程进程=新进程();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;尝试{process.Start();StreamReader 阅读器 = process.StandardOutput;字符串输出 = reader.ReadToEnd();}捕获(异常异常){AddComment(exception.ToString());}

但是当我运行这个时,我得到:

<块引用>

process.Start()中出现系统找不到指定的文件"错误;没有process.StartInfo.UseShellExecute = false;process.StartInfo.RedirectStandardOutput = true;

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

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

解决方案

我怀疑问题在于您指定的文件名是相对于您的工作目录的,而您期望的是 Process.Start 在启动过程时查看那里 - 我不相信当 UseShellExecutefalse 时它不会这样工作.尝试仅指定要启动的进程的绝对文件名:

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

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

I am trying to get the process respond as a string so I can use it in different place in my code, this is the solution that I have so far:

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());
      }

But when I'm running this I get:

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

The code runs fine but it just open console window and all the process response is trow there so I can't use it as string.

Does anyone know why I am getting this error or maybe a different solution to my problem?

解决方案

I suspect the problem is that the filename you're specifying is relative to your working directory, and you're expecting Process.Start to look there when starting the process - I don't believe it works that way when UseShellExecute is false. Try just specifying the absolute filename of the process you want to start:

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

Note that I've also removed the space from the end of the string you were assigning for the FileName property - it's entirely possible that was casuing the problem too.

这篇关于“系统找不到指定的文件"process.Start() 上的错误;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆