如何从 C# 启动进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/181719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:54:38  来源:igfitidea点击:

How do I start a process from C#?

c#.netwindowsprocessprocess.start

提问by

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

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

回答by Matt Hamilton

You can use the System.Diagnostics.Process.Startmethod to start a process. You can even pass a URL as a string and it'll kick off the default browser.

您可以使用System.Diagnostics.Process.Start方法来启动进程。您甚至可以将 URL 作为字符串传递,它会启动默认浏览器。

回答by Franci Penov

Use the Processclass. The MSDN documentation has an example how to use it.

使用Process类。MSDN 文档有一个如何使用它的示例。

回答by GvS

Just as Matt says, use Process.Start.

正如马特所说,使用Process.Start

You can pass a URL, or a document. They will be started by the registered application.

您可以传递 URL 或文档。它们将由注册的应用程序启动。

Example:

例子:

Process.Start("Test.Txt");

This will start Notepad.exe with Text.Txt loaded.

这将启动 Notepad.exe 并加载 Text.Txt。

回答by Andy McCluggage

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...

正如马特汉密尔顿所建议的那样,您对流程的控制有限的快速方法是在 System.Diagnostics.Process 类上使用静态 Start 方法......

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

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.

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

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.

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

回答by Blackvault

I used the following in my own program.

我在自己的程序中使用了以下内容。

Process.Start("http://www.google.com/etc/etc/test.txt")

It's a bit basic, but it does the job for me.

这有点基础,但它对我有用。

回答by AVIK DUTTA

Include the using System.Diagnostics;.

包括 using System.Diagnostics;.

And then call this Process.Start("Paste your URL string here!");

然后调用这个 Process.Start("Paste your URL string here!");

Try something like this:

尝试这样的事情:

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace btnproce
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string t ="Balotelli";
            Process.Start("http://google.com/search?q=" + t);
        }
    }
}

Please note that it is a sample ASP.NET page as an example. You should try and improvise a little bit.

请注意,它是一个示例 ASP.NET 页面作为示例。你应该尝试即兴发挥一点。

回答by SimperT

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/YourSubDirectory/yourprogram.exe");
Process.Start(new ProcessStartInfo(path));

回答by alireza amini

You can use this syntax for running any application:

您可以使用此语法来运行任何应用程序:

System.Diagnostics.Process.Start("Example.exe");

And the same one for a URL. Just write your URL between this ().

与 URL 相同。只需在这之间写下您的 URL ()

Example:

例子:

System.Diagnostics.Process.Start("http://www.google.com");

回答by Carla Jesus

Declare this

声明这个

[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
[DllImport("user32")]
private static extern bool ShowWindowAsync(IntPtr hwnd, int a);

And put this inside your function (note that "checkInstalled" is optional, but if you'll use it, you have to implement it)

并将其放入您的函数中(请注意,“checkInstalled”是可选的,但如果您要使用它,则必须实现它)

if (ckeckInstalled("example"))
{
    int count = Process.GetProcessesByName("example").Count();
    if (count < 1)
        Process.Start("example.exe");
    else
    {
        var proc = Process.GetProcessesByName("example").FirstOrDefault();
        if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
        {
            SetForegroundWindow(proc.MainWindowHandle);
            ShowWindowAsync(proc.MainWindowHandle, 3);
        }
    }
}

NOTE:I'm not sure if this works when more than one instance of the .exe is running.

注意:当多个 .exe 实例正在运行时,我不确定这是否有效。

回答by Ravi Kumar G N

class ProcessStart
{
    static void Main(string[] args)
    {
        Process notePad = new Process();

        notePad.StartInfo.FileName   = "notepad.exe";
        notePad.StartInfo.Arguments = "ProcessStart.cs";

        notePad.Start();
    }
}