Execute Command using Process - CSharp System.Diagnostics

CSharp examples for System.Diagnostics:Process

Description

Execute Command using Process

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.IO;// w ww  .j a v  a 2 s .c o  m
using System.Diagnostics;
using System.Collections.Generic;
using System;
using NUnit.Framework;

public class Main{
        static internal void ExecuteCommand(string[] args, string workingDirectory, string fileName = "cmd.exe")
        {
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = fileName,
                Arguments = string.Join(" ", args),
                WorkingDirectory = workingDirectory,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false
            };
            Console.WriteLine($"command = {startInfo.Arguments}");
            var proc = Process.Start(startInfo);
            Console.WriteLine(proc.StandardOutput.ReadToEnd());
            Console.WriteLine(proc.StandardError.ReadToEnd());
            proc.WaitForExit();
        }
}

Related Tutorials