Execution shell command using Process - CSharp System.Diagnostics

CSharp examples for System.Diagnostics:Process

Description

Execution shell command using Process

Demo Code


using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from w  ww  .  j ava2  s. c  o  m

public class Main{

        public static string Execution(string command)
        {

            Process p = new Process();

            p.StartInfo.FileName = CMDUrl;
            p.StartInfo.UseShellExecute = false;   
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            p.StandardInput.WriteLine(command + "&exit");

            p.StandardInput.AutoFlush = true;
            //p.StandardInput.WriteLine("exit");
            string output = p.StandardOutput.ReadToEnd();
            string error = p.StandardError.ReadToEnd();
            p.WaitForExit();
            p.Close();
            return error;
        }
}

Related Tutorials