已得到了一个CMD窗口的句柄,怎么样可以获取该CMD窗口里的内容呢?
该CMD窗口的内容是: dir c: /s 输出的内容.
我想即时逐行读取.

解决方案 »

  1.   

    Process p = new Process();
    p.StartInfo.FileName = @"cmd.exe";p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardError = true;


    try
    {
    p.Start();
    p.StandardInput.WriteLine("dir d:");
    p.StandardInput.WriteLine("exit");
    p.WaitForExit();
    string output = p.StandardOutput.ReadToEnd();
             p.Close();
    Console.WriteLine("The following is the output\n"+output);
    }
    catch(System.Exception e)
    {
    Console.Out.WriteLine(e.Message);
    }
    Console.Out.WriteLine(p.StandardError.ReadToEnd());output里面是命令执行的结果。
      

  2.   

    不是这样!
    我要的那个CMD窗口,不是由我的软件执行的,
    是别的软件执行的.我可以得到 句柄.
    就是不知道怎么样可以得到内容
      

  3.   

    to 是别的软件执行的.我可以得到 句柄.
    就是不知道怎么样可以得到内容如果知道窗体句柄,可以通过
    API: GetWindowThreadProcessId 
    获得进程ID,
    参看
    http://www.pinvoke.net/default.aspx/user32/GetWindowThreadProcessId.html然后通过Process.GetProcessByID获得进程,
    最后通过上面的方法进行读取输出信息即可。
      

  4.   

    api -->GetWindowText??
    -----------这个不行。大家来思考一下,如何解决类似telnet的命令,这类命令的特点:与用户有交互。
      

  5.   

    telnet的交互与显示,是telnet自己处理的。楼主的,是自己处理别人的,没有可比性吧?
      

  6.   

    call api
    ms-help://MS.MSDNQTR.2003FEB.2052/dllproc/base/console_buffer_security_and_access_rights.htm