当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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.

网友答案:

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

Hope this helps someone

分享给朋友:
您可能感兴趣的文章:
随机阅读: