定义执行程序的工作目录(C#)

Jam*_*mes 7 .net c# windows

我目前正在尝试从特定文件夹启动可执行文件.

我下面的代码奇怪地崩溃了应用程序:

Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = s;
p.Start();
Run Code Online (Sandbox Code Playgroud)

我调试它,它说它找不到要启动的文件,但文件/文件夹defintly存在,我的语法不好?

下面的代码可以工作,但是没有定义工作的directroy,因此无法找到可执行文件

Process.Start(@"dump\", s);
Run Code Online (Sandbox Code Playgroud)

Bri*_*ian 13

您设置的工作目录("转储")是相对于当前工作目录的.您可能想要检查当前的工作目录.

您应该能够使用此代码将工作目录设置为执行程序集目录...

string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Directory.SetCurrentDirectory(exeDir);
Run Code Online (Sandbox Code Playgroud)

或者,更好的是,不要使用相对路径,将p.StartInfo.WorkingDirectory设置为绝对路径.