[英]Process.Start() not starting the .exe file (works when run manually)
我有一个.exe
文件,需要在创建文件后运行。 文件已成功创建,我之后使用以下代码运行.exe
文件:
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = pathToMyExe;
processInfo.ErrorDialog = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
Process proc = Process.Start(processInfo);
我也试过一个简单的Process.Start(pathToMyExe);
但.exe
文件未运行。 当我在Windows资源管理器上手动尝试pathToMyExe
,程序正确运行。 但不是通过该计划。 我看到的是光标转向等待几秒然后恢复正常。 所以也没有抛出异常。 什么阻止文件?
您没有设置工作目录路径,与通过资源管理器启动应用程序时不同,它不会自动设置为可执行文件的位置。
做这样的事情:
processInfo.WorkingDirectory = Path.GetDirectoryName(pathToMyExe);
(假设输入文件,DLL等在该目录中)
private void Print(string pdfFileName)
{
string processFilename = Microsoft.Win32.Registry.LocalMachine
.OpenSubKey("Software")
.OpenSubKey("Microsoft")
.OpenSubKey("Windows")
.OpenSubKey("CurrentVersion")
.OpenSubKey("App Paths")
.OpenSubKey("AcroRd32.exe")
.GetValue(string.Empty).ToString();
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = processFilename;
info.Arguments = string.Format("/p /h {0}", pdfFileName);
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
////(It won't be hidden anyway... thanks Adobe!)
info.UseShellExecute = false;
Process p = Process.Start(info);
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
int counter = 0;
while (!p.HasExited)
{
System.Threading.Thread.Sleep(1000);
counter += 1;
if (counter == 5)
{
break;
}
}
if (!p.HasExited)
{
p.CloseMainWindow();
p.Kill();
}
}
由于工作目录不同,您必须将工作目录正确设置为您希望进程启动的路径。
这样的示例演示可以是:
Process process = new Process()
{
StartInfo = new ProcessStartInfo(path, "{Arguments If Needed}")
{
WindowStyle = ProcessWindowStyle.Normal,
WorkingDirectory = Path.GetDirectoryName(path)
}
};
process.Start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.