public static void Execute(string command, string paramsIdent, System.Diagnostics.ProcessWindowStyle windowStyle, bool wait) { System.Diagnostics.Process NuevoProceso = new System.Diagnostics.Process(); NuevoProceso.StartInfo = new System.Diagnostics.ProcessStartInfo(command, paramsIdent); NuevoProceso.StartInfo.WindowStyle = windowStyle; if (command.IndexOf(".exe", StringComparison.OrdinalIgnoreCase) < 0) { NuevoProceso.StartInfo.UseShellExecute = true; } else { NuevoProceso.StartInfo.UseShellExecute = false; } NuevoProceso.Start(); if (wait) { NuevoProceso.WaitForExit(); } }
/// <summary> /// 启动外部应用程序 /// </summary> /// <param name="appName">应用程序路径名称</param> /// <param name="arguments">启动参数</param> /// <param name="style">进程窗口模式</param> /// <returns></returns> public static bool StartApp(string appName, string arguments, System.Diagnostics.ProcessWindowStyle style) { bool result = false; using (System.Diagnostics.Process p = new System.Diagnostics.Process()) { p.StartInfo.FileName = appName;//exe,bat and so on p.StartInfo.WindowStyle = style; p.StartInfo.Arguments = arguments; try { p.Start(); p.WaitForExit(); p.Close(); result = true; } catch { } } return(result); }
private void ExecucteCustomToolWindow(string toolGroup, string toolName, guiAssetTreeTag tag) { string infoFilename = mCustomToolsInfo.Replace("*", toolGroup); MOG_Ini customTools = new MOG_Ini(infoFilename); string command = ""; string argAsset = ""; System.Diagnostics.ProcessWindowStyle windowMode = System.Diagnostics.ProcessWindowStyle.Normal; bool createWindow = false; if (customTools.SectionExist(toolName)) { // Get command if (customTools.KeyExist(toolName, "Command")) { command = FormatString(tag, customTools.GetString(toolName, "Command")); } // Get argAsset if (customTools.KeyExist(toolName, "argAsset")) { argAsset = FormatString(tag, customTools.GetString(toolName, "argAsset")); } // Get window mode if (customTools.KeyExist(toolName, "windowMode")) { string mode = FormatString(tag, customTools.GetString(toolName, "windowMode")); if (string.Compare(mode, "Hidden", true) == 0) { windowMode = System.Diagnostics.ProcessWindowStyle.Hidden; } else if (string.Compare(mode, "Maximise", true) == 0) { windowMode = System.Diagnostics.ProcessWindowStyle.Maximized; } else if (string.Compare(mode, "Minimized", true) == 0) { windowMode = System.Diagnostics.ProcessWindowStyle.Minimized; } else if (string.Compare(mode, "Normal", true) == 0) { windowMode = System.Diagnostics.ProcessWindowStyle.Normal; } else { windowMode = System.Diagnostics.ProcessWindowStyle.Normal; } } // Get Toggle Options if (customTools.KeyExist(toolName, "ToggleOptions")) { createWindow = true; } // Get Numerical Options if (customTools.KeyExist(toolName, "NumericalOptions")) { createWindow = true; } // Get String Options if (customTools.KeyExist(toolName, "StringOptions")) { createWindow = true; } if (createWindow) { //CustomToolOptionsForm form = new CustomToolOptionsForm(); //form.Text = toolName; //if (form.ShowDialog() == DialogResult.OK) //{ // Do stuff //} } else { string output = ""; Report outputForm = new Report(mainForm); if (windowMode == System.Diagnostics.ProcessWindowStyle.Hidden) { outputForm.LogRichTextBox.Text = "GENERATING REPORT. PLEASE WAIT..."; outputForm.LogOkButton.Enabled = false; // Load saved positions //mainForm.mUserPrefs.Load("ReportForm", outputForm); guiUserPrefs.LoadDynamic_LayoutPrefs("ReportForm", outputForm); outputForm.Show(mainForm); Application.DoEvents(); } guiCommandLine.ShellSpawn(command.Trim(), argAsset.Trim(), windowMode, ref output); if (windowMode == System.Diagnostics.ProcessWindowStyle.Hidden) { outputForm.LogRichTextBox.Text = output; outputForm.LogOkButton.Enabled = true; } } } }
/// <summary> /// Overload for Shell() Function that Allows Overloading of the Working directory Variable. /// It must be a String but can be variables that returns strings. /// </summary> /// <returns>0, process stdout data, process stderr data</returns> public static object Shell(string FileName, string Arguments, bool RedirectStandardOutput, bool RedirectStandardError, bool UseShellExecute, bool CreateNoWindow, System.Diagnostics.ProcessWindowStyle WindowStyle, string WorkingDirectory, bool WaitForProcessExit) { object ret = 0; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = FileName; proc.StartInfo.Arguments = Arguments; proc.StartInfo.RedirectStandardOutput = RedirectStandardOutput; proc.StartInfo.RedirectStandardError = RedirectStandardError; proc.StartInfo.UseShellExecute = UseShellExecute; proc.StartInfo.CreateNoWindow = CreateNoWindow; proc.StartInfo.WindowStyle = WindowStyle; proc.StartInfo.WorkingDirectory = WorkingDirectory; proc.Start(); if (RedirectStandardError) { ret = proc.StandardError.ReadToEnd(); } if (RedirectStandardOutput) { ret = proc.StandardOutput.ReadToEnd(); } if (WaitForProcessExit) { proc.WaitForExit(); } // Required to have Detection on the process running to work right. return(ret); }
/// <summary> /// ショートカット作成 /// </summary> /// <param name="shortcutPath">作成するショートカットのフルパス</param> /// <param name="targetLinkPath">ショートカットのリンク先</param> /// <param name="iocnPath">アイコンのファイル名</param> /// <param name="iconIndex">アイコンのインデックス番号</param> /// <param name="workingDirectory">作業フォルダ</param> /// <param name="arguments">コマンドパラメータ 「リンク先」の後ろに付く </param> /// <param name="description">コメント</param> /// <param name="hotkey">ホットキー</param> /// <param name="windowStyle">起動時のウィンドウの状態</param> public static void CreateShortcut(string shortcutPath, string targetLinkPath, string iocnPath = "", int iconIndex = 0, string workingDirectory = "", string arguments = "", string description = "", string hotkey = "", System.Diagnostics.ProcessWindowStyle windowStyle = System.Diagnostics.ProcessWindowStyle.Normal) { //WshShellを作成 Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); dynamic shell = Activator.CreateInstance(t); //WshShortcutを作成 var shortcut = shell.CreateShortcut(shortcutPath); //リンク先 shortcut.TargetPath = targetLinkPath; //アイコン if (iocnPath.Equals("")) { //ショートカットのリンク先ファイルから shortcut.IconLocation = targetLinkPath + "," + iconIndex.ToString(); } else { //アイコンは別ファイルから shortcut.IconLocation = iocnPath + "," + iconIndex.ToString(); } //作業フォルダ if (!workingDirectory.Equals("")) { shortcut.WorkingDirectory = workingDirectory; } //コマンドパラメータ 「リンク先」の後ろに付く if (!arguments.Equals("")) { shortcut.Arguments = arguments; } //ショートカットキー(ホットキー) //ex "Ctrl+Alt+Shift+F12" if (!hotkey.Equals("")) { shortcut.Hotkey = hotkey; } //コメント if (!description.Equals("")) { shortcut.Description = description; } //起動時のウィンドウの状態 //System.Diagnostics.ProcessWindowStyle.Maximized --- 最大化されたウィンドウスタイルを指定します。 //System.Diagnostics.ProcessWindowStyle.Minimized --- 最小化されたウィンドウスタイルを指定します。 //System.Diagnostics.ProcessWindowStyle.Normal --- 通常の表示ウィンドウスタイルを指定します。デフォルト。 shortcut.WindowStyle = windowStyle; //ショートカットを作成 shortcut.Save(); //後始末 System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell); }
/// <summary> /// Overload for Shell() Function that Allows Overloading of the Working directory Variable. /// It must be a String but can be variables that returns strings. /// </summary> /// <returns>0, process stdout data, process stderr data</returns> internal static object Shell(string FileName, string Arguments, bool RedirectStandardOutput, bool RedirectStandardError, bool UseShellExecute, bool CreateNoWindow, System.Diagnostics.ProcessWindowStyle WindowStyle, string WorkingDirectory, bool WaitForProcessExit) { object ret = 0; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = FileName; proc.StartInfo.Arguments = Arguments; proc.StartInfo.RedirectStandardOutput = RedirectStandardOutput; proc.StartInfo.RedirectStandardError = RedirectStandardError; proc.StartInfo.UseShellExecute = UseShellExecute; proc.StartInfo.CreateNoWindow = CreateNoWindow; proc.StartInfo.WindowStyle = WindowStyle; proc.StartInfo.WorkingDirectory = WorkingDirectory; proc.Start(); // so that way main form Test mods functionality actually works (Lame ass hack I think tbh)... if (ExecutingElsword) { ExecutingElsword = false; } if (RedirectStandardError) { ret = proc.StandardError.ReadToEnd(); } if (RedirectStandardOutput) { ret = proc.StandardOutput.ReadToEnd(); } if (WaitForProcessExit) { proc.WaitForExit(); } // Required to have Detection on the process running to work right. return(ret); }
private string EjecutarAplicacionAndGetOutputWithLetInput(string app, string workingDir, string parametros, out string comandoEjecutado, out System.Diagnostics.Process actualExeProcess, System.Diagnostics.ProcessWindowStyle win_style = System.Diagnostics.ProcessWindowStyle.Hidden) { comandoEjecutado = ""; System.Diagnostics.Process process = new System.Diagnostics.Process(); actualExeProcess = exe_process = process; System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); process.StartInfo.FileName = app; process.StartInfo.WorkingDirectory = workingDir; // directorio de trabajo // extP.StartInfo.Arguments = parametros + " -y -stats"; // extP.StartInfo.Arguments = parametros + " -y"; process.StartInfo.Arguments = parametros; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; // process.StartInfo.RedirectStandardInput = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.ErrorDialog = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = win_style; process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized; comandoQueSeEjeuta = comandoEjecutado = process.StartInfo.FileName + " " + process.StartInfo.Arguments; outputErrorString += "exe line:\r\n" + comandoEjecutado + "\r\n\r\n"; process.OutputDataReceived += ExtP_OutputDataReceived; process.ErrorDataReceived += ExtP_ErrorDataReceived; startTime = DateTime.Now.ToString("yyyy.MM.dd. HH:mm:ss.ffff"); timeOn.Restart(); process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); //int coresTotales = System.Environment.ProcessorCount; //extP.ProcessorAffinity = (IntPtr)(1 /*+ 2 + 4 + 8 + 16 + 32 + 64 + 128*/); try { process.WaitForExit(); } catch (Exception ex) { } timeOn.Stop(); endTime = DateTime.Now.ToString("yyyy.MM.dd. HH:mm:ss.ffff"); process.Close(); comandoQueSeEjeuta += $"elapsed time: ({timeOn.ElapsedMilliseconds:N0} ms)"; return(outputString + "\r\n" + outputErrorString + "\r\n"); }