请问一下C#程序里面怎么执行命令行程序,并且获得返回信息???比如像ipconfig

解决方案 »

  1.   

    在cmd下执行啊,直接在exe文件所在目录下输入exe文件的文件名就可以啊。
      

  2.   

    如果你要像ipconfig一样,在任意目录下都可以运行的话,就要设置exe文件的环境变量了。
      

  3.   

    你写一个console的程序,把结果写到comand line里就可以了,运行吗可已注册环境变量,要不就只只能在当前目录运行
      

  4.   

    don't consider the dos promot environment,they belong to different process. 
    you should know about its protocol or using win32 api that may provide help for you.many examples in internet can help you like how to use ping in c#.
    http://wacle.blogchina.com/359322.html
      

  5.   

    下面是前两天别人的一个帖子,一下子找不到了,直接贴code吧:
    using System;using System.Diagnostics;namespace ZZ{ class ZZConsole { [STAThread] static void Main(string[] args) {     string ip = "127.0.0.1"; string strRst = CmdPing(ip); return;
    } private static string CmdPing(string strIp) { 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();
    //Console.WriteLine(p.HandleCount.ToString()); p.StandardInput.WriteLine("ping -n 1 "+strIp); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd(); //Console.WriteLine(strRst);
    p.Close(); return strRst; } }}