-
-
Save uzbekdev1/15591bc4757d2b44f3d0c943e1bb17e7 to your computer and use it in GitHub Desktop.
Process Start Helper C#
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
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