[英]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.