使用cmd.exe执行命令时出现问题

时间:2014-03-10 16:48:07

标签: c# cmd

执行此代码时遇到问题:

Process arp = new Process();
arp.StartInfo.UseShellExecute = false;
arp.StartInfo.RedirectStandardOutput = true;
arp.StartInfo.FileName = "C://Windows//System32//cmd.exe";
arp.StartInfo.Arguments = "arp -a";
arp.StartInfo.RedirectStandardOutput = true;
arp.Start();
arp.WaitForExit();
string output = arp.StandardOutput.ReadToEnd();
MessageBox.Show(output);

程序应该将arp-a的输出放入String输出中,但它反过来给了我这个:

  

Microsoft Windows [版本6.1.7601] \ r \ n版权所有(c)2009 Microsoft Corporation。保留所有权利。\ r \ n \ r \ nC:\ Users \ user01 \ documents \ visual studio 2012 \ Projects \ freehotspotexploiter \ freehotspotexploiter \ bin \ Debug>

有人知道如何修复它吗?

2 个答案:

答案 0 :(得分:4)

要将命令发送到cmd.exe作为参数,请使用“/ c”标志。

arp.StartInfo.Arguments = "/c arp -a";

答案 1 :(得分:1)

您的命令名称应为arp而不是cmd.exe

替换它:

arp.StartInfo.FileName = "C://Windows//System32//cmd.exe";
arp.StartInfo.Arguments = "arp -a";

有了这个:

arp.StartInfo.FileName = "arp";
arp.StartInfo.Arguments = "-a";
相关问题