通过控制台应用程序打开具有特定URL的浏览器

use*_*010 1 windows url console

我正在Visual Studio中进行控制台应用程序,但我有一点问题.如果我想在按下任何键时打开带有指定URL的浏览器,我该怎么办?

谢谢

czl*_*tea 17

如果您还想涵盖 .Net Core 应用程序。感谢 Brock Allen

https://brockallen.com/2016/09/24/process-start-for-urls-on-net-core/

public static void OpenBrowser(string url)
{
    try
    {
        Process.Start(url);
    }
    catch
    {
        // hack because of this: https://github.com/dotnet/corefx/issues/10361
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            url = url.Replace("&", "^&");
            Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            Process.Start("xdg-open", url);
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
        {
            Process.Start("open", url);
        }
        else
        {
            throw;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Gri*_*nov 6

使用ProcessStartInfo类实例来设置用于启动进程的值.

像这样的东西:

using System;
using System.Diagnostics;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var psi = new ProcessStartInfo("iexplore.exe");
            psi.Arguments = "http://www.google.com/";
            Process.Start(psi);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 您可以将所有Main简化为一行:`System.Diagnostics.Process.Start(@"http:// URL");` (5认同)