C#调用CMD命令后显示命令后信息

private   void   button1_Click(object   sender,   EventArgs   e) 
{ 
      try 
      { 
              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   172.16.1.1 "); 
              p.StandardInput.WriteLine( "exit "); 
              string   strRst   =   p.StandardOutput.ReadToEnd(); 
              p.WaitForExit(); 
              this.label1.Text   =   strRst; 
        } 
          catch   (Exception   err) 
          { 
                MessageBox.Show(err.Message);   //显示错误信息。 
        } 
} 

posted @ 2011-07-28 23:55  小锋神  阅读(1968)  评论(0编辑  收藏  举报