startInfo.WorkingDirectory 处理中

startInfo.WorkingDirectory in Process

谁能解释一下 ProcessStartInfo 中的 startInfo.WorkingDirectory 是什么,不知道如何使用它。

var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "folder\code.exe");

我想要运行一个.exe文件

ProcessStartInfo startInfo = new ProcessStartInfo(Environment.ExpandEnvironmentVariables(filePath));
startInfo.WorkingDirectory = Environment.ExpandEnvironmentVariables(@"%AppData%\folder\");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);

有什么区别

ProcessStartInfo startInfo = new ProcessStartInfo(filePath);
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);

基本上它设置了 exe 的工作目录。想想一个 windows 快捷方式,它在那里也有一个工作目录。工作目录主要包含 "home" 的路径,更重要的是,它控制应用程序首先查找 DLL 文件的位置。如果我没记错我的路径规则,它会搜索应用程序的工作目录、system、system32 以及 PATH 环境变量中的任何目录。

希望对您有所帮助!

工作目录是它开始的地方,在你的第二个例子中,它从你当前目录所在的地方开始,在第一个例子中,你得到一个文件夹 applicationdata,(所以 expandenvironmentalvariables 可能不是必需的,因为 %windows% 等)但是,您在 code.exe 的文件夹中启动 exe,在第二个它是您的代码用作其工作文件夹的应用程序的位置..

假设您当前的应用程序是 app.exe,它位于 c:\myapp

你启动你的应用程序,它目前在 c:\myapp 中运行,因为你可以从资源管理器中 运行 它,或者这是捷径。

在示例一中,您的进程将转到您的 user\appliationdata\folder\ 和 运行 code.exe

在示例二中,它将 运行 code.exe 来自 c:\myapp