日期:2014-05-18  浏览次数:21459 次

C#调用telnet命令无法取得返回值
大家好,请教一个问题。

在C#中调用telnet命令,但是代码执行后 没有得到telnet的返回的值(使用ping命令可以得到)。
请问如何能得到它的返回值?

下面是代码:

  private static string CmdTelnet(string strIp)
  {
  string pingrst="";
  string strRst = "";
  Process p = new Process();

  //设定程序名
  p.StartInfo.FileName = "cmd.exe";
  //关闭Shell的使用
  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("telnet " + strIp);
  p.StandardInput.WriteLine("exit");

  strRst = p.StandardOutput.ReadToEnd();

  p.Close();

  return pingrst;
  }

希望各位朋友不吝赐教,谢谢。


------解决方案--------------------
p.WaitForExit();

然后获取 ExitCode