如何通过C#打开cmd并执行.exe文件 [英] How to open cmd via c# and execute .exe files

查看:573
本文介绍了如何通过C#打开cmd并执行.exe文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我想通过C#打开cmd,然后要更改cmd中的目录并执行一些.exe文件.

hi every body
i want to open cmd via c# and then want to change the directory in cmd and execute some .exe file.

Process p = new Process();
        string result = "";

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = tru

推荐答案

您不应该这样做.

流程类 [ StartInfo [
You do not what to do that.

Play around with Process class[^] and StartInfo[^]. There you can specify what startup directory, parameter and what .exe file you what to startup. No need to use CMD for that.

An example of how to use Process.Start with arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com");


ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);


如果您只想执行一个exe文件,并且要以相同的方式打开cmd,那么我建议直接执行该exe.这是代码.

If you just want to execute an exe file and you want to open cmd for the same so i would suggest directly executing the exe. Here is the code.

System.Diagnostics.Process.Start("Path","Arguments");


运行另一个进程应该是最后的选择.
我不建议使用IExplore进行此操作,因为在该进程运行时您将无法与它进行通信.

有更好的方法:在UI应用程序中使用Web浏览器控件System.Windows.Forms.WebBrowser:
http://msdn.microsoft.com/en-us/library/system. windows.forms.webbrowser.aspx [ ^ ].

—SA
Running another process should be the last resort.
I do not recommend doing this with IExplore, because you won''t have a way of communicating with this process when it''s running.

There is much better way: use the Web browser control System.Windows.Forms.WebBrowser in you UI application:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx[^].

—SA


这篇关于如何通过C#打开cmd并执行.exe文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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