Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The system cannot find the file specified" error on process.Start();

Tags:

c#

process

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?

like image 855
Daria Shalimov Avatar asked Jul 09 '15 07:07

Daria Shalimov


2 Answers

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.

like image 109
Jon Skeet Avatar answered Nov 18 '22 10:11

Jon Skeet


For System32 access if you are trying to RUN an x86 Application on x64 then you must use the "Sysnative" keyword instead of "System32" in your filename.

EG: instead of:

C:\Windows\System32\whoiscl.exe

It should be:

C:\Windows\Sysnative\whoiscl.exe

Hope this helps someone

like image 31
f4r4 Avatar answered Nov 18 '22 10:11

f4r4