如何在C#中正确设置进程WorkingDirectory

Nis*_*lon 5 c# process

在设置进程时,似乎我没有以正确的方式使用该变量WorkingDirectory.我得到了错误(有一个问题)

ApplicationName ='Test.exe',CommandLine ='/ d = 1',CurrentDirectory ='C:\ Users\mb\Desktop\Integration\Tests\dailyTest\dailyTest\bin\Debug\Stress',Native error =系统不能找到指定的文件.

但是在Stress文件夹中,我确实有Test.exe ..我真的没有得到这个的含义.

代码如下(注意我用直接字符串内容替换变量以便更好地理解).

Process proc = new System.Diagnostics.Process();

proc.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory() + "\\" + "Stress");
proc.StartInfo.FileName = "Test.exe";
proc.StartInfo.Arguments = "/d=1";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start ();
proc.WaitForExit();

return proc.ExitCode;
Run Code Online (Sandbox Code Playgroud)

我知道WorkingDirectory受UseShellExecute影响,但我尊重这一点.

Der*_*rek 0

Directory.Exists( proc.StartInfo.WorkingDirectory )设置好后尝试添加。Test.exe该目录中存在 吗?

还可以尝试:

string filename = Path.Combine( Directory.GetCurrentDirectory(), "Stress", "test.exe" );

check File.Exists( filename );
Run Code Online (Sandbox Code Playgroud)

filename也许用作proc.StartInfo.FileName

对于您的工作目录使用:Path.Combine( Directory.GetCurrentDirectory(), "Stress" )

为了澄清我会说使用:

proc.StartInfo.WorkingDirectory = Path.Combine( Directory.GetCurrentDirectory(),  "Stress");
proc.StartInfo.FileName = Path.Combine( Directory.GetCurrentDirectory(),  "Stress", "Test.exe" );
bool folderExists = Directory.Exists( proc.StartInfo.WorkingDirectory );
bool fileExists = File.Exists( proc.StartInfo.FileName );
Run Code Online (Sandbox Code Playgroud)

您可以调试查看文件和文件夹是否存在。