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.
See more on this question at Stackoverflow