整一个窗体,上面放个TextBox和Button.
TextBox里面输入IP地址,点击Button后,在另外一个TextBox里显示结果。我在网上查到不少资料,都是讲怎样在C#里调用命令啥的。
就用System.Diagnostics.Process调用命令。
但是所有的例子都是用“Ping”命令。同样的程序,我用“Ping”时,确实可以把结果获得并放到TextBox里。但是,换做“Tracert”时,就完不成了。而且比较奇怪的是,一点按钮就又弹出个一模一样的窗体,
这是为什么啊?有没有弄过的教教偶?谢过啦先~~~~~~~

解决方案 »

  1.   

    应该和Ping一样的,贴出你的代码看看
      

  2.   

    Process process = new Process();
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow =true;
    process.Start();
    process.StandardInput.WriteLine("ping " + this.textBox1.Text);
    process.StandardInput.WriteLine("exit");
    string output = process.StandardOutput.ReadToEnd();
    process.Close();
    this.textBox2.Text = output;Ping 命令时,textBox2的最终结果是:
    Microsoft Windows XP [版本 5.1.2600]
    (C) 版权所有 1985-2001 Microsoft Corp.D:\Tracert\Tracert\bin\Debug>ping www.sina.com.cn
    Pinging antares.sina.com.cn [218.30.13.35] with 32 bytes of data:
    Reply from 218.30.13.35: bytes=32 time=137ms TTL=49
    Reply from 218.30.13.35: bytes=32 time=132ms TTL=49
    Reply from 218.30.13.35: bytes=32 time=149ms TTL=49
    Reply from 218.30.13.35: bytes=32 time=181ms TTL=49
    Ping statistics for 218.30.13.35:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 132ms, Maximum = 181ms, Average = 149ms
    D:\Tracert\Tracert\bin\Debug>exitTracert 命令时:弹出一个新的窗体,然后就象死机了一样
      

  3.   

    哦,错了。更正!Tracert 命令时:居然会弹出一个新的窗口,过一会儿把新弹出的窗口关闭,
    最开始的那个窗口里的textBox2里的内容是:
    Microsoft Windows XP [版本 5.1.2600]
    (C) 版权所有 1985-2001 Microsoft Corp.
    D:\Tracert\Tracert\bin\Debug>tracert www.sina.com.cn
    D:\Tracert\Tracert\bin\Debug>exit中间的tracert内容都没有了而且还弹出个新窗口,这是最让我难以容忍的事。
      

  4.   

    System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
    Info.Arguments = textBox1.Text;
    Info.FileName = "Tracert.exe";
    Info.RedirectStandardOutput = true;
    Info.UseShellExecute = false;
    Process pro = Process.Start(Info);
    MessageBox.Show(pro.StandardOutput.ReadToEnd());
      

  5.   

    测试通过了
    System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
    Info.Arguments = textBox1.Text; // 这是你要测试的ip地址
    Info.FileName = "Tracert.exe";
    Info.RedirectStandardOutput = true;
    Info.UseShellExecute = false;
    Process pro = Process.Start(Info);
    MessageBox.Show(pro.StandardOutput.ReadToEnd());// pro.StandardOutput.ReadToEnd()这是运行结果Info.RedirectStandardOutput = true;
    Info.UseShellExecute = false;
    这两句是关键,把输出结果重定向
      

  6.   

    我运行的是再系统里面添加一个用户的命令,结果没有成功,最后突然想到了原因
    你可以因ping而不能直接用tracert的原因非常可能是权限的问题..
      

  7.   

    .net应用程序运行的时候,用的是ASPNET帐户,属于user组,执行很多命令的时候都收到限制
      

  8.   

    linuxyf(率人哥哥)的回复好象有一点问题,应该加一句process.StartInfo.CreateNoWindow =true;
      

  9.   

    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=208497
      

  10.   

    ff0619(ff),Tracert.exe本来就是控制台程序,根本用不着process.StartInfo.CreateNoWindow =true;这句
      

  11.   

    不好意思,前几天忘记结贴啦。linuxyf(率人哥哥) 提供的代码已经解决了我的问题。谢谢大家的回复,嘿嘿。