C#调用cmd程序,读取结果

示例,调用cmd执行PING命令,读取结果,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

namespace AppUserDocsExecute
{
    class Program
    {
        static void Main(string[] args)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            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("ping -n 1 SHAWN-PC");
            p.StandardInput.WriteLine("exit");
            string strRst = p.StandardOutput.ReadToEnd();
            p.Close();
            Console.WriteLine(strRst);
            Console.ReadLine();
        }
    }
}

执行结果,如下图:

Result

原文地址:https://www.cnblogs.com/shawnzhou/p/3282226.html