c# 调用外部程序的例子

 

#region 一个调用外部程序的例子
  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;
   string pingrst;
   p.Start();
   p.StandardInput.WriteLine("telnet 192.168.3.175 9944");
   p.StandardInput.WriteLine("quit");
   p.StandardInput.WriteLine("exit");
   string strRst = p.StandardOutput.ReadToEnd();
   Trace.WriteLine(strRst);
   p.Close();
   return pingrst;
   }
#endregion

习惯改为;private static string Cmd(string strcmd)
{
 Process p = new Process();
 p.StartInfo.FileName = "cmd.exe /c"+strcmd;
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardInput = true;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.RedirectStandardError = true;
 p.StartInfo.CreateNoWindow = true;
 p.Start();www.shengfang.org
 string strRst = p.StandardOutput.ReadToEnd();
 System.Diagnostics.Trace.WriteLine(strRst);
 p.Close();
 return strRst;
}

posted @ 2005-11-17 10:40  大洋  阅读(774)  评论(0编辑  收藏  举报