windows 从 C# 打开程序 - 还指定工作目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6239662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:59:52  来源:igfitidea点击:

Open Program from C# - also specifying the working directory

c#.netwindows

提问by Dan

I have some code that launches an external program, although is it possible to specify the working directory, as the external program is a console program:

我有一些启动外部程序的代码,尽管可以指定工作目录,因为外部程序是一个控制台程序:

Code:

代码:

private void button5_Click_2(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(@"update\update.exe");
    }

回答by Dyppl

Yes, it's possible, use ProcessStartInfoobject to specify all the params you need and then just pass it to the Startmethod like that:

是的,有可能,使用ProcessStartInfoobject 来指定您需要的所有参数,然后将其传递给这样的Start方法:

...
using System.Diagnostics;
...

var psi = new ProcessStartInfo(@"update\update.exe");
  psi.WorkingDirectory = @"C:\workingDirectory";
Process.Start(psi);

回答by Richard Slater

You can specify the Working Directory using ProcessStartInfo.WorkingDirectory.

您可以使用ProcessStartInfo.WorkingDirectory指定工作目录。

...
using System.Diagnostics;
...

var processStartInfo = new ProcessStartInfo(@"explorer.exe");
  processStartInfo.WorkingDirectory = @"C:\";
var process = Process.Start(processStartInfo);