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

标签 windows url console

我正在 Visual Studio 中做一个控制台应用程序,但我遇到了一个小问题。 如果我想在按下任意键时打开具有指定 URL 的浏览器,我该怎么做?

谢谢

最佳答案

如果您还想涵盖 .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;
        }
    }
}

关于windows - 通过控制台应用程序打开具有特定 URL 的浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14982746/

相关文章:

c++ - 异常情况下关闭线程

windows - Windows 7 x64 的 shell 扩展 dll 中未调用 InvokeCommand

java - 通过HttpURLConnection访问HTTP网站的问题

php - 从 URL 获取路径

wpf - 使用 WindowsformsHost 将报表查看器插入 WPF 应用程序时出现问题

c - 调试和堆分配

Django如何定义通用url

c# - Console当前行怎么写?

linux - 在 Linux 中使用 Ctrl-Alt-F6,无法恢复屏幕

Java 控制台 JPanel