Using cmd.exe from C#

こ雲淡風輕ζ 提交于 2019-12-08 11:44:27

问题


I have been toying with the Process class in C#. I have some code below I'd like to use to open cmd.exe and run the DIR command. However, when I attempt to use the code, the cmd.exe opens, but no command is run. Why is this happening and how do I fix it?

Process cmd = new Process();
cmd.StartInfo.FileName = @"cmd.exe";
cmd.StartInfo.Arguments = @"DIR";
cmd.Start();
cmd.WaitForExit();

回答1:


Try passing the /K option to let the command console stay at video and receive the subsequent DIR command (Without exit).

Process cmd = new Process();
cmd.StartInfo.FileName = @"cmd.exe";
cmd.StartInfo.Arguments = @"/K DIR";  // <-- This will execute the command and wait to close
cmd.Start();
cmd.WaitForExit();

The /K option will allow you to get a better understanding of what happens in the command window because the window will not close immediately and you need to click the close button or type the Exit command. If you want to exit after issuing your commands then use the /C option.




回答2:


cmd.StartInfo.Arguments = @"/c DIR";



来源:https://stackoverflow.com/questions/16696953/using-cmd-exe-from-c-sharp

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