积极答复者
C#中调用系统默认浏览器打开url的最好方法是什么?

问题
答案
-
Hi liubin,也许你客户电脑上的所有默认浏览器程序都是不相同的。
你可以尝试使用下面的代码查找用户默认的浏览器,然后对应程序打开网页。
string browserName = "iexplore.exe"; object progIdValue = GetDefaultBrowserPath(); if (progIdValue != null) { if (progIdValue.ToString().ToLower().Contains("chrome")) browserName = "chrome.exe"; else if (progIdValue.ToString().ToLower().Contains("firefox")) browserName = "firefox.exe"; else if (progIdValue.ToString().ToLower().Contains("safari")) browserName = "safari.exe"; else if (progIdValue.ToString().ToLower().Contains("opera")) browserName = "opera.exe"; } Process.Start(new ProcessStartInfo(browserName, "www.google.com")); public static string GetDefaultBrowserPath() { string defaultBrowserPath = null; RegistryKey regkey; // Check if we are on Vista or Higher OperatingSystem OS = Environment.OSVersion; if ((OS.Platform == PlatformID.Win32NT) && (OS.Version.Major >= 6)) { regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false); if (regkey != null) { defaultBrowserPath = regkey.GetValue("Progid").ToString(); } else { regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes\\IE.HTTP\\shell\\open\\command", false); defaultBrowserPath = regkey.GetValue("").ToString(); } } else { regkey = Registry.ClassesRoot.OpenSubKey("http\\shell\\open\\command", false); defaultBrowserPath = regkey.GetValue("").ToString(); } return defaultBrowserPath; }
Best Regards,
Yohann Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 liubin 2017年9月20日 9:10
全部回复
-
Hi liubin,也许你客户电脑上的所有默认浏览器程序都是不相同的。
你可以尝试使用下面的代码查找用户默认的浏览器,然后对应程序打开网页。
string browserName = "iexplore.exe"; object progIdValue = GetDefaultBrowserPath(); if (progIdValue != null) { if (progIdValue.ToString().ToLower().Contains("chrome")) browserName = "chrome.exe"; else if (progIdValue.ToString().ToLower().Contains("firefox")) browserName = "firefox.exe"; else if (progIdValue.ToString().ToLower().Contains("safari")) browserName = "safari.exe"; else if (progIdValue.ToString().ToLower().Contains("opera")) browserName = "opera.exe"; } Process.Start(new ProcessStartInfo(browserName, "www.google.com")); public static string GetDefaultBrowserPath() { string defaultBrowserPath = null; RegistryKey regkey; // Check if we are on Vista or Higher OperatingSystem OS = Environment.OSVersion; if ((OS.Platform == PlatformID.Win32NT) && (OS.Version.Major >= 6)) { regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false); if (regkey != null) { defaultBrowserPath = regkey.GetValue("Progid").ToString(); } else { regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes\\IE.HTTP\\shell\\open\\command", false); defaultBrowserPath = regkey.GetValue("").ToString(); } } else { regkey = Registry.ClassesRoot.OpenSubKey("http\\shell\\open\\command", false); defaultBrowserPath = regkey.GetValue("").ToString(); } return defaultBrowserPath; }
Best Regards,
Yohann Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 liubin 2017年9月20日 9:10