我正在运行一个工具,它对硬件 PCI 进行采样以获得特定值(我没有写它)。
当我从命令提示符运行它时,它会返回一个退出代码(正确的),但是当我使用从另一个应用程序运行它时Process.Start
,它会返回另一个退出代码。
直接运行应用程序还是通过运行应用程序有区别Process.Start
吗?您知道此问题的简单解决方法吗?
我正在运行一个工具,它对硬件 PCI 进行采样以获得特定值(我没有写它)。
当我从命令提示符运行它时,它会返回一个退出代码(正确的),但是当我使用从另一个应用程序运行它时Process.Start
,它会返回另一个退出代码。
直接运行应用程序还是通过运行应用程序有区别Process.Start
吗?您知道此问题的简单解决方法吗?
As stated in Hassan's answer (which solved my similar issue), the exit code returned from Process.Start() is affected by the location of the executable, in particular which directory it is located in. Here's the code I used:
string yourExe = "C\\Program Files\\Your Directory\\YourExe.exe";
string currentDir = Directory.GetCurrentDirectory();
string yourExeDir = "C\\Program Files\\Your Directory";
try
{
Directory.SetCurrentDirectory(yourExeDir);
}
catch (DirectoryNotFoundExeption dnfe)
{
MessageBox.Show("The specified directory does not exist. " + dnfe.Message);
}
if (!File.Exists(yourExe))
{
MessageBox.Show("Can't find yourExe");
}
else
{
Process.Start(yourExe);
}
try
{
//Set the current directory.
Directory.SetCurrentDirectory(currentDir);
}
catch (DirectoryNotFoundException dnfe)
{
MessageBox.Show("The specified directory does not exist. " + dnfe.Message);
}
This switches the current working directory to the directory where the .exe is located, runs it, and then switches back to whatever your previous working directory was.
如果您想获得相同的结果Process.Start()
,您必须在与命令行相同的工作目录中执行您的应用程序。