9

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?

Wtower
  • 18,848
  • 11
  • 103
  • 80
Daria Shalimov
  • 131
  • 1
  • 1
  • 8

2 Answers2

20

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @DariaShalimov if this is the answer to your question, mark as answer – Jamaxack Feb 19 '16 at 12:15
  • @DariaShalimov: There should be a tick mark next to the score for the answer, on the left - just click on the tick mark to accept the answer. – Jon Skeet Mar 13 '16 at 13:07
  • 1
    Can you explain in more detail why setting the `UseShellExecute` to `false` would change the behavior with `WorkingDirectory`? I expect that if I set a working directory and a file name that it would use the working directory to execute that file. – John Grabanski Apr 25 '16 at 20:44
  • @JohnGrabanski: From the documentation for `UseShellExecute`: "The WorkingDirectory property behaves differently depending on the value of the UseShellExecute property. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, it is assumed that the current directory contains the executable." – Jon Skeet Apr 25 '16 at 20:59
6

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

Community
  • 1
  • 1
f4r4
  • 563
  • 8
  • 19