我的代码            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("telnet xxx.xxx.xxx.xxx");
            
            p.StandardInput.WriteLine("exit");            string strRst = p.StandardOutput.ReadToEnd();为什么在strRst里拿不到telnet命令之后的输出, 或者有什么方法可以周转拿到, 应该在telnet的那台服务器上,我还需要敲入一些命令, 而这些命令也是会有返回值的, 我也希望能拿到这些值, 谢谢

解决方案 »

  1.   

    可以参看这个例子
    http://www.codeproject.com/cs/miscctrl/shellcontrol.asp主要参看捕获console输出部分。
      

  2.   

    用DOS的输出重定向(或曰管道重定向)功能应该可以,把TELNET向屏幕的输出指到向一个文本文件的输出,之后去读那个文本文件.
    重定向一般是用>,<,>>等符号,有兴趣可以自己查阅一下.
      

  3.   

    上面都没有看清LZ的问题,他已经设定了重定向。我觉得你直接使用Telnet.exe可能还行
      

  4.   

    不知道你问题出在哪里,你可以用我这个例子例如这个可以:
    public static bool TestRun(string cmd,ref string str) 

    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;  try 

    p.Start(); 
    p.StandardInput.Write(cmd); 
    p.StandardInput.Write(p.StandardInput.NewLine);
    p.StandardInput.WriteLine("exit");

    str = p.StandardOutput.ReadToEnd(); 
    p.Close(); 
    return true; 

    catch (Exception ex)

    return false; 

    }
      

  5.   

    p.StandardInput.WriteLine("telnet xxx.xxx.xxx.xxx");
    关键是这句命令之后, 我还需要去输入user name和password, 但拿不到那个窗口了
      

  6.   

    或者大家有什么比较好的telnet client library for C#, 推荐一下, 谢谢
      

  7.   

    你直接用telnet,不用cmd.exe看看可以不?