/// <summary> /// build a process wrapper that wraps a process runned within the provided command line arguments /// </summary> /// <param name="CommandLine"></param> /// <returns></returns> public static ProcessWrapper Run(string fileName, string arguments, sys.ProcessWindowStyle windowStyle) { var psi = new sys.ProcessStartInfo(fileName, arguments) { WindowStyle = windowStyle }; return(Run(psi)); }
/// <summary> /// 运行一个进程并等待它执行完成 可指定窗口样式 /// </summary> /// <param name="cmd">进程</param> /// <param name="arguments">参数</param> /// <param name="winStyle">窗口状态 最大化 最小化 隐藏</param> /// <returns>如果进程出错返回错误日志</returns> public static string RunWait(string cmd, System.Diagnostics.ProcessWindowStyle winStyle, string arguments = "") { cmd = "\"" + cmd + "\""; using (System.Diagnostics.Process p = new System.Diagnostics.Process()) { p.StartInfo.FileName = cmd; p.StartInfo.Arguments = arguments; p.StartInfo.WindowStyle = winStyle; p.StartInfo.UseShellExecute = true; try { p.Start(); p.WaitForExit(); p.Close(); return(string.Empty); } catch (Exception ex) { return(ex.ToExceptionDetail()); } } }
/// <summary> /// 运行一个进程并等待它执行完成 可指定窗口样式 /// </summary> /// <param name="cmd">进程</param> /// <param name="arguments">参数</param> /// <param name="winStyle">窗口状态 最大化 最小化 隐藏</param> /// <returns>如果进程出错返回错误日志</returns> public static IntPtr RunWaitHandle(string cmd, System.Diagnostics.ProcessWindowStyle winStyle, string arguments = "") { cmd = "\"" + cmd + "\""; using (System.Diagnostics.Process p = new System.Diagnostics.Process()) { p.StartInfo.FileName = cmd; p.StartInfo.Arguments = arguments; p.StartInfo.WindowStyle = winStyle; p.StartInfo.UseShellExecute = true; try { p.Start(); p.WaitForExit(); p.Close(); return(p.MainWindowHandle); } catch { return(IntPtr.Zero); } } }
// \param string command : command line, including parameters // \param string currentDirectory : working directory ('null' if not defined) // \param int waitForExit : if > 0, the method waits for the end of the process. The value 'waitForExit' is also the timeout (unit:msec). // \param System.Diagnostics.ProcessWindowStyle p_windowStyle: the window style (Minimized, Maximized, ...) // \param out string errMsg // Return: 'false' in caso di errore // Error management: by error code public static bool CreateProcess(string command, string currentDirectory, int waitForExit, out string errMsg, System.Diagnostics.ProcessWindowStyle p_windowStyle) { errMsg = ""; System.Diagnostics.Process process = new System.Diagnostics.Process(); try { string exe = Util.StrListGetFirst(ref command, ' '); process.StartInfo.FileName = exe; process.StartInfo.Arguments = command; process.StartInfo.WorkingDirectory = currentDirectory; process.StartInfo.WindowStyle = p_windowStyle; process.Start(); } catch (Exception ex) { errMsg = ex.Message; return(false); // Error } if (waitForExit > 0) { while ((waitForExit > 0) && (process.HasExited == false)) { System.Threading.Thread.Sleep(100); waitForExit -= 100; } if (process.HasExited == false) // If timeout ... { errMsg = "Process timeout"; return(false); // Error } } return(true); // Ok }