使用process.start运行应用程序会返回与手动运行应用程序不同的值
问题描述:
我正在运行一个工具,用于为特定值(我没有写入它)对HW PCI进行采样。使用process.start运行应用程序会返回与手动运行应用程序不同的值
当我从命令提示符运行它时,它会返回一个退出代码(正确的),但是当我使用Process.Start
从另一个应用程序运行它时,它会返回另一个退出代码。
直接运行应用程序还是通过Process.Start
运行应用程序有区别吗? 你知道这个问题的一个简单的解决方法吗?
答
如果你想得到与Process.Start()
相同的结果,你必须在你的命令行上执行你的应用程序 相同的工作目录。
答
正如Hassan的回答(解决了我的类似问题)中所述,从Process.Start()返回的退出代码受可执行文件位置影响,特别是它所在的目录。下面是我使用的代码:
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);
}
此切换当前工作目录到.exe文件所在的目录,运行它,然后再切换回无论你以前的工作目录。
没有任何真实的细节,谁知道?什么工具?你有源吗?它有文档吗?它是否描述退出代码?环境是否一样?等等,等等。 – leppie 2012-07-22 11:15:41
*你打电话给'Process.Start'?* – Adam 2012-07-22 11:16:13
该工具是公司内部的。 – 2012-07-22 11:19:05