使用代码在CMD中执行命令 [英] Execute a command in CMD with code

查看:85
本文介绍了使用代码在CMD中执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想打开cmd并发送"systeminfo"给它,以获得一些系统信息,
但是这段代码不起作用:

Hi,
I want to open cmd and send "systeminfo" to it in order to get some system information,
but this code doesn''t work :

private void Form1_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd";
            p.StartInfo.Arguments = "systeminfo";
            p.Start();
        }


我该怎么办?
谢谢.


what should I do?
Thanks.

推荐答案

不是启动新的cmd,而是尝试简单地启动 msinfo32.exe ,这将启动系统信息"窗口.如果您需要命令行参数,请查看此文档: http://support.microsoft.com/kb/300887 [^ ]
Instead of starting a new cmd, try simply starting the msinfo32.exe which will launch the System Information window. And if you need command line arguments, have a look at this document: http://support.microsoft.com/kb/300887[^]


对于您提供的示例,只需将FileName属性设置为 systeminfo.exe .您还应该研究Process类.您可能需要在调用Process.Start()之前设置其他属性.
For the example you provided, just set the FileName property to systeminfo.exe. You should also research the Process class a bit more. You probably need other properties to be set before calling Process.Start().


您的所有代码所要做的就是启动命令.它不会记录返回到任何地方的信息,并且您的代码不会从您启动的过程中读取StdOut.

最重要的是,通过正确应用WMI查询,您可以拥有从SystemInfo获得的所有信息.您根本不需要阅读和解析任何外部命令.
All your code does is launch the command. It doesn''t log the information returned anywhere and your code doesn''t read StdOut of the process you launched.

On top of that, all of the information you get from SystemInfo can be had with a proper application of WMI queries. You don''t need to read and parse any external command at all.


这篇关于使用代码在CMD中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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