使用cmd.exe从C# [英] Using cmd.exe from C#

查看:190
本文介绍了使用cmd.exe从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩C#中的Process类。我有一些代码下面我想使用打开cmd.exe和运行DIR命令。但是,当我尝试使用代码,cmd.exe打开,但没有运行命令。为什么会发生这种情况,如何解决?

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();


推荐答案

尝试通过 / K 选项让命令控制台留在视频并接收后续的DIR命令(无退出)。

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.

这篇关于使用cmd.exe从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆