问题
I've got an issue executing this code:
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);
The program should put the output of arp-a into the String output but it gives me back this instead:
Microsoft Windows [Version 6.1.7601]\r\nCopyright (c) 2009 Microsoft Corporation. All rights reserved.\r\n\r\nC:\Users\user01\documents\visual studio 2012\Projects\freehotspotexploiter\freehotspotexploiter\bin\Debug>
Someone knows how to fix it?
回答1:
In order to send a command to cmd.exe as an argument, use the "/c" flag.
arp.StartInfo.Arguments = "/c arp -a";
回答2:
Your command name should be arp
not cmd.exe
Replace This:
arp.StartInfo.FileName = "C://Windows//System32//cmd.exe";
arp.StartInfo.Arguments = "arp -a";
With This:
arp.StartInfo.FileName = "arp";
arp.StartInfo.Arguments = "-a";
来源:https://stackoverflow.com/questions/22306258/issue-executing-a-command-with-cmd-exe