"The system cannot find the specified file." Error in process.Start ();

I am trying to make the process respond as a string, so I can use it elsewhere in my code, this is the solution that I still have:

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 run 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 works fine, but it just opens a console window, and the whole process response is that I cannot use it as a string.

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

+5
source share
2 answers

I suspect that the problem is that the file name you specify refers to your working directory, and you expect Process.Start to look there when the process starts - I don't think this works when UseShellExecute false . Try simply specifying the absolute file name of the process you want to run:

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

Note that I also removed the space from the end of the line that you assigned to the FileName property, and it is possible that this also caused a problem.

+12
source

For System32 access, if you are trying to run an x86 application on x64, you must use the keyword "Sysnative" instead of "System32" in your file name.

EG: instead of:

C: \ Windows \ System32 \ whoiscl.exe

It should be:

C: \ Windows \ Sysnative \ whoiscl.exe

Hope this helps someone

+4
source

All Articles