如何用C#启动一个进程?

197

当用户点击按钮时,我应该如何启动进程,例如打开一个URL?

15个回答

262

正如Matt Hamilton所建议的那样,如果你对进程控制有限,则可以使用System.Diagnostics.Process类的静态Start方法进行快速处理...

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

另一种选择是使用 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.

这种方法比我提到的更加具有控制性。


我使用这种方法通过Outlook打开.msg文件,但是当我尝试打开另一个.msg文件时,它会打开另一个Outlook。有没有办法重用当前进程并避免打开另一个Outlook?http://stackoverflow.com/questions/28534358/open-msg-file-using-process-start - user1166085
9
你应该使用using语句或者手动释放进程资源。关于Process.Dispose()方法的作用,请参考这个链接:https://dev59.com/2mQn5IYBdhLWcg3wRlYM - Hoppe

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

19

正如Matt所说,使用Process.Start

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

示例:

Process.Start("Test.Txt");

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


4
如果没有为这种类型注册程序会发生什么? - L C
2
@LC Win32Exception (0x80004005) "没有与此操作关联的指定文件的应用程序" - Yousha Aleayoub

12

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

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

虽然很基础,但对我来说已经足够了。


1
当像您的示例一样与URL一起使用时,这也具有使用系统默认的Web浏览器打开URL的优点。 - Lemonseed

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

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

        notePad.Start();
    }
}

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

5
您可以使用以下语法来运行任何应用程序:
System.Diagnostics.Process.Start("Example.exe");

同样的方式可以用于处理URL。只需要将你的URL放在这个()之间即可。

示例:

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

5
使用 Process 类。MSDN 文档中有一个使用它的示例。

5

如果在Windows系统上使用:

Process process = new Process();
process.StartInfo.FileName = "Test.txt";
process.Start();

适用于 .Net Framework,但对于 Net core 3.1,还需要将 UseShellExecute 设置为 true。

Process process = new Process();
process.StartInfo.FileName = "Test.txt";
process.StartInfo.UseShellExecute = true;
process.Start();

1
使用 System.Diagnostics; - Lemons

3

声明这个

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

将以下内容放在你的函数中(请注意,“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);
        }
    }
}

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接