在Windows窗体应用里怎么调用 Tracert 命令啊?

ljb2000 2006-02-27 04:15:44
整一个窗体,上面放个TextBox和Button.
TextBox里面输入IP地址,点击Button后,在另外一个TextBox里显示结果。

我在网上查到不少资料,都是讲怎样在C#里调用命令啥的。
就用System.Diagnostics.Process调用命令。
但是所有的例子都是用“Ping”命令。

同样的程序,我用“Ping”时,确实可以把结果获得并放到TextBox里。

但是,换做“Tracert”时,就完不成了。而且比较奇怪的是,一点按钮就又弹出个一模一样的窗体,
这是为什么啊?有没有弄过的教教偶?

谢过啦先~~~~~~~

...全文
184 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljb2000 2006-03-06
  • 打赏
  • 举报
回复
不好意思,前几天忘记结贴啦。

linuxyf(率人哥哥) 提供的代码已经解决了我的问题。

谢谢大家的回复,嘿嘿。
giggsjw 2006-02-28
  • 打赏
  • 举报
回复
学习
linuxyf 2006-02-28
  • 打赏
  • 举报
回复
ff0619(ff),Tracert.exe本来就是控制台程序,根本用不着process.StartInfo.CreateNoWindow =true;这句
真相重于对错 2006-02-27
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=208497
ff0619 2006-02-27
  • 打赏
  • 举报
回复
linuxyf(率人哥哥)的回复好象有一点问题,应该加一句process.StartInfo.CreateNoWindow =true;
lizi02 2006-02-27
  • 打赏
  • 举报
回复
.net应用程序运行的时候,用的是ASPNET帐户,属于user组,执行很多命令的时候都收到限制
lizi02 2006-02-27
  • 打赏
  • 举报
回复
我运行的是再系统里面添加一个用户的命令,结果没有成功,最后突然想到了原因
你可以因ping而不能直接用tracert的原因非常可能是权限的问题..
lizi02 2006-02-27
  • 打赏
  • 举报
回复
以前我也遇到了这样的问题
linuxyf 2006-02-27
  • 打赏
  • 举报
回复
楼主,揭帖吧
linuxyf 2006-02-27
  • 打赏
  • 举报
回复
测试通过了
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;
这两句是关键,把输出结果重定向
linuxyf 2006-02-27
  • 打赏
  • 举报
回复
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());
ljb2000 2006-02-27
  • 打赏
  • 举报
回复
哦,错了。更正!

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内容都没有了。。。而且还弹出个新窗口,这是最让我难以容忍的事。


ljb2000 2006-02-27
  • 打赏
  • 举报
回复
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>exit

Tracert 命令时:弹出一个新的窗体,然后就象死机了一样。。。。。。
linuxyf 2006-02-27
  • 打赏
  • 举报
回复
应该和Ping一样的,贴出你的代码看看

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧