Created
December 24, 2014 14:41
-
-
Save lski/36df372a979dc58fb225 to your computer and use it in GitHub Desktop.
Simple functions for working with processes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using Dia = System.Diagnostics; | |
/// <summary> | |
/// Simple functions for working with processes | |
/// </summary> | |
public static class Process { | |
/// <summary> | |
/// Runs an external program as a system process, using the command line text passed and waits until the external program | |
/// has finished before continuing. By Default it runs the process as a hidden program. | |
/// </summary> | |
/// <param name="processCommand"></param> | |
/// <param name="windowBehaviour"></param> | |
/// <remarks></remarks> | |
[DebuggerStepperBoundary] | |
public static void RunToComplete(string processCommand, ProcessWindowStyle windowBehaviour = ProcessWindowStyle.Hidden) { | |
Dia.Process process = null; | |
try { | |
process = new Dia.Process(); | |
} | |
catch (Exception ex) { | |
throw new Exception("There was an error creating a new process object", ex); | |
} | |
try { | |
process.StartInfo.FileName = processCommand; | |
if (windowBehaviour == ProcessWindowStyle.Hidden) { | |
process.StartInfo.UseShellExecute = false; | |
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; | |
} | |
else { | |
process.StartInfo.WindowStyle = windowBehaviour; | |
} | |
// Start and wait until the process passes back an exit code | |
process.Start(); | |
process.WaitForExit(); | |
} | |
catch (Exception ex) { | |
throw new Exception("There was an error when running the process " + processCommand, ex); | |
} | |
finally { | |
// Free resources associated with this process | |
process.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment