Process.Start()没有启动.exe文件(手动运行时工作)

我有一个.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 ,程序正确运行。 但不是通过该计划。 我看到的是光标转向等待几秒然后恢复正常。 所以也没有抛出exception。 什么阻止文件?

您没有设置工作目录路径,与通过资源管理器启动应用程序时不同,它不会自动设置为可执行文件的位置。

做这样的事情:

 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();