Skip to content

Instantly share code, notes, and snippets.

@uzbekdev1
Forked from gokulraja/ProcessHelper.cs
Created June 5, 2018 20:24
Show Gist options
  • Save uzbekdev1/15591bc4757d2b44f3d0c943e1bb17e7 to your computer and use it in GitHub Desktop.
Save uzbekdev1/15591bc4757d2b44f3d0c943e1bb17e7 to your computer and use it in GitHub Desktop.
Process Start Helper C#
private static void ProcessHelper(string fileName, string arguments, bool showResultsinConsole)
{
// Start the child process.
using (var p = new Process{
StartInfo =
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
FileName = fileName
}
})
{
// Redirect the output stream of the child process.
if (!string.IsNullOrEmpty(arguments))
p.StartInfo.Arguments = arguments;
if(!string.IsNullOrEmpty(Path.GetDirectoryName(fileName)))
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(fileName);
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
if (showResultsinConsole)
Console.WriteLine(output);
p.WaitForExit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment