gpt4 book ai didi

c# - 正确设置进程工作目录

转载 作者:太空狗 更新时间:2023-10-29 23:50:38 25 4
gpt4 key购买 nike

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

ApplicationName='Test.exe', CommandLine='/d=1',CurrentDirectory='C:\Users\mb\Desktop\Integration\Tests\dailyTest\dailyTest\bin\Debug\Stress',Native error= The system cannot find the file specified.

但是,在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 的影响,但我尊重这一点。

最佳答案

设置后尝试添加 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 );

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

关于c# - 正确设置进程工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com