我以通常的方式启动可执行文件:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WorkingDirectory = @"C:\someDirectory\";
p.StartInfo.FileName = "ConsoleProgram.exe";
p.Start();
目前,可执行文件不在 C:\someDirectory\ 中,因此我希望上述操作因未找到可执行文件而失败。
相反,它所做的是,由于某些原因,它在我的桌面上找到了同名的不同可执行文件! (我知道这一点是因为如果我在桌面上重命名 .exe,软件会提示找不到 .exe。)我如何防止这种行为,并强制执行特定路径而不是其他路径?
请您参考如下方法:
When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used by the process that is started and only has meaning within the context of the new process.
这里您将 UseShellExecute 设置为 false,WorkingDirectory 毫无意义,因此您必须使用绝对路径作为 FileName。
