我正在 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/