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

时间:2015-05-27 13:11:32

标签: c# process

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

  

ApplicationName =' Test.exe',CommandLine =' / d = 1',   currentDirectory所=' C:\用户\ MB \桌面\集成\测试\ dailyTest \ dailyTest \ BIN \调试\胁迫&#39 ;,   本机错误=系统找不到指定的文件。

然而在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;

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

2 个答案:

答案 0 :(得分:0)

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

还可以尝试:

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

check File.Exists( filename );

也许使用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 );

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

答案 1 :(得分:0)

发现我的错误:

我在Linux上,所以我需要指定我的exe文件由“mono”执行

process.FileName = "mono"
process.Argument = "nameOfExe param1 param2..."