C# .NET Process.Start 默认目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/114928/
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
.NET Process.Start default directory?
提问by Brian Knoblauch
I'm firing off a Java application from inside of a C# .NETconsole application. It works fine for the case where the Java application doesn't care what the "default" directory is, but fails for a Java application that only searches the current directory for support files.
我正在从 C# .NET控制台应用程序内部触发 Java 应用程序。它适用于 Java 应用程序不关心“默认”目录是什么的情况,但对于仅搜索当前目录以查找支持文件的 Java 应用程序失败。
Is there a process parameter that can be set to specify the default directory that a process is started in?
是否可以设置进程参数来指定启动进程的默认目录?
采纳答案by Dror Helper
Yes! ProcessStartInfo Has a property called WorkingDirectory, just use:
是的!ProcessStartInfo 有一个名为WorkingDirectory的属性,只需使用:
...
using System.Diagnostics;
...
var startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = // working directory
// set additional properties
Process proc = Process.Start(startInfo);
回答by Ben Hoffstein
回答by Simon Johnson
The Process.Start method has an overload that takes an instance of ProcessStartInfo. This class has a property called "WorkingDirectory".
Process.Start 方法具有采用 ProcessStartInfo 实例的重载。这个类有一个名为“WorkingDirectory”的属性。
Set that property to the folder you want to use and that should make it start up in the correct folder.
将该属性设置为您要使用的文件夹,这应该使它在正确的文件夹中启动。
回答by Joseph Daigle
Use the ProcessStartInfo
class and assign a value to the WorkingDirectory
property.
使用ProcessStartInfo
该类并为该WorkingDirectory
属性赋值。
回答by Larry Smithmier
Use the ProcessStartInfo.WorkingDirectoryproperty to set it prior to starting the process. If the property is not set, the default working directory is %SYSTEMROOT%\system32.
在启动进程之前使用ProcessStartInfo.WorkingDirectory属性设置它。如果未设置该属性,则默认工作目录为 %SYSTEMROOT%\system32。
You can determine the value of %SYSTEMROOT% by using:
您可以使用以下方法确定 %SYSTEMROOT% 的值:
string _systemRoot = Environment.GetEnvironmentVariable("SYSTEMROOT");
Here is some sample code that opens Notepad.exe with a working directory of %ProgramFiles%:
下面是一些使用 %ProgramFiles% 工作目录打开 Notepad.exe 的示例代码:
...
using System.Diagnostics;
...
ProcessStartInfo _processStartInfo = new ProcessStartInfo();
_processStartInfo.WorkingDirectory = @"%ProgramFiles%";
_processStartInfo.FileName = @"Notepad.exe";
_processStartInfo.Arguments = "test.txt";
_processStartInfo.CreateNoWindow = true;
Process myProcess = Process.Start(_processStartInfo);
There is also an Environment variable that controls the current working directory for your process that you can access directly through the Environment.CurrentDirectoryproperty .
还有一个 Environment 变量,用于控制您的进程的当前工作目录,您可以通过Environment.CurrentDirectory属性直接访问该目录。
回答by CBBSpike
Just a note after hitting my head trying to implement this. Setting the WorkingDirectory value does not work if you have "UseShellExecute" set to false.
在我试图实现这一点后,只是一个便条。如果您将“UseShellExecute”设置为 false,则设置 WorkingDirectory 值不起作用。