如何从 C# 启动进程? [英] How do I start a process from C#?

查看:42
本文介绍了如何从 C# 启动进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何启动进程,例如在用户单击按钮时启动 URL?

How do I start a process, such as launching a URL when the user clicks a button?

推荐答案

正如 Matt Hamilton 所建议的,当您对流程的控制有限时,快速方法是使用 System.Diagnostics.Process 类上的静态 Start 方法...

As suggested by Matt Hamilton, the quick approach where you have limited control over the process, is to use the static Start method on the System.Diagnostics.Process class...

using System.Diagnostics;
...
Process.Start("process.exe");

另一种方法是使用 Process 类的实例.这允许对进程进行更多控制,包括调度、运行窗口的类型,以及等待进程完成的能力,对我来说最有用.

The alternative is to use an instance of the Process class. This allows much more control over the process including scheduling, the type of the window it will run in and, most usefully for me, the ability to wait for the process to finish.

using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.

这种方法允许的控制比我提到的要多得多.

This method allows far more control than I've mentioned.

这篇关于如何从 C# 启动进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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