Shawn

智慧本身就是好的。有一天我们都会死去,追求智慧的道路还会有人在走着。死掉以后的事我看不到,但在我活着的时候,想到这件事,心里就很高兴。 —— 王小波

导航

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

posted on 2013-08-26 11:36  ShawnZhou  阅读(456)  评论(0编辑  收藏  举报