Tuesday 6 August 2019

Execute command and get cmd output in C#

Given below code snippet will help us to execute any command line in background and trace output.
For example, we want to know git status of current working directory.


//Console.WriteLine(CommandOutput("git status"));
 
public static string CommandOutput(string command,
                                   string workingDirectory = null)
{
    try
    {
        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd""/c " + command);
 
        procStartInfo.RedirectStandardError = procStartInfo.RedirectStandardInput = procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        if (null != workingDirectory)
        {
            procStartInfo.WorkingDirectory = workingDirectory;
        }
 
        Process proc = new Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
 
        StringBuilder sb = new StringBuilder();
        proc.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e)
        {
            sb.AppendLine(e.Data);
        };
        proc.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e)
        {
            sb.AppendLine(e.Data);
        };
 
        proc.BeginOutputReadLine();
        proc.BeginErrorReadLine();
        proc.WaitForExit();
        return sb.ToString();
    }
    catch (Exception objException)
    {
        return $"Error in command: {command}{objException.Message}";
    }
}