Issue executing a command with cmd.exe

◇◆丶佛笑我妖孽 提交于 2019-12-11 12:47:39

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!