1.在一個項目中,一個Library 調用另外一個Library的可執行文件時,如果用Process.Start(exe文件)(如果該exe文件沒有相關的配置文件,則可以執行成功),但是如果有相關的配置文件,則會出現該exe文件已停止工作的錯誤提示。出現這樣的原因是這樣的,本身調用的時候,目錄是自己bin/debug文件夾的路徑,之后你設置其他路徑的時候,就會出現錯誤,可以用cmd調試測試下,直接打開cmd,將exe要調用的程序放入cmd中執行,看是否出錯,如果不出錯,那么使用Process.Start("exe文件")調用也沒問題的,如果出錯,則是路徑的問題。
解決方法:程序操作cmd來調用exe程序,先cd到exe程序的所在目錄
代碼如下:
Process p = new Process();//新進程 p.StartInfo.FileName = "cmd.exe";//打開cmd程序 p.StartInfo.UseShellExecute = false;//不使用shell啟動程序 p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true;//true表示不顯示黑框,false表示顯示dos界面 try { p.Start();//啟動 p.StandardInput.WriteLine(@"cd\"); p.StandardInput.WriteLine(@"cd " + AppDomain.CurrentDomain.BaseDirectory + "ProWin" + "");//執行程序所在目錄 p.StandardInput.WriteLine(@"" + AppDomain.CurrentDomain.BaseDirectory + @"ProWin\WindowsUI.exe" + "");//執行程序具體位置 p.StandardInput.WriteLine("exit");//退出 p.Close();//關閉 } catch (Exception ex) { }