- Call method body
public void OpenBrowser() { string url = "http://localhost:8055/api"; bool openRes = OpenBrowserHelper. OpenChromeBrowserUrl(url); if (!openRes) openRes = OpenBrowserHelper. OpenDefaultBrowserUrl(url); if (!openRes) { // Open the download link, download from the official website Google if (MessageBox.Show("Google (Chrome) browser is not installed in the system, do you want to download and install it?", null, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes) { OpenBrowserHelper.OpenIe("https://www.google.cn/chrome/"); } } }
- Open browser help class
public class OpenBrowserHelper { ///
/// Call Google (Chrome) browser /// /// Open the link of the web page public static bool OpenChromeBrowserUrl(string url) { bool isOpen = true; try { // Open Google Chrome with Google, if not found, use the default browser of the system // Google has been uninstalled, the registry has not been cleared, the program will return a "The system cannot find the specified file." bug var chromeKey = @"\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe"; // Find the Google Chrome installation path through the registry string chromeAppFileName = (string)(Registry. GetValue("HKEY_LOCAL_MACHINE" + chromeKey, "", null) ?? Registry. GetValue("HKEY_CURRENT_USER" + chromeKey, "", null)); // Find Google Chrome and open it if (!string.IsNullOrWhiteSpace(chromeAppFileName)) { Process.Start(chromeAppFileName, url); } else { isOpen = false; // open by default browser //OpenDefaultBrowserUrl(url); } } catch { isOpen = false; } return isOpen; } ////// Call IE browser /// /// public static void OpenIe(string url) { // IE browser path installation: C:\Program Files\Internet Explorer // at System.Diagnostics.process.StartWithshellExecuteEx(ProcessStartInfo startInfo) notice this error try { if (File. Exists(@"C:\Program Files\Internet Explorer\iexplore.exe")) { ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = @"C:\Program Files\Internet Explorer\iexplore.exe", Arguments = url, UseShellExecute = false, CreateNoWindow = true }; Process.Start(processStartInfo); } else { if (File. Exists(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe")) { ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe", Arguments = url, UseShellExecute = false, CreateNoWindow = true }; Process.Start(processStartInfo); } } } catch (Exception exception) { } } ////// Call the system's default browser (the user has set the default browser himself) /// /// public static bool OpenDefaultBrowserUrl(string url) { bool isOpen = true; try { //Read the default browser executable file path from the registry RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\"); if (key != null) { string browserPath = string. Empty; string[] splitArr = new string[] { }; string browser = key. GetValue(""). ToString(); //browser is the default browser, the parameters behind different browsers"It's not the same. For example: "D:\Program Files (x86)\Google\chrome.exe" -- "%1" var lastIndex = browser.IndexOf(".exe", StringComparison.Ordinal); if (lastIndex == -1) lastIndex = browser.IndexOf(".EXE", StringComparison.Ordinal); if (lastIndex != -1) { splitArr = browser.Split("\""); } // Greater than 0, it means cut to data according to " if (splitArr. Length > 0) { browserPath = splitArr[1]; } else if (splitArr.Length == 0 && lastIndex != -1) //Indicates that there is a browser, such as: D:\QQBrowser\QQBrowser.exe { browserPath = browser; } // open the browser var result = Process. Start(browserPath, url); // call IE //if (result == null) // OpenIe(url); } else { isOpen = false; //OpenIe(url); } } catch { isOpen = false; } return isOpen; } }
C# calls the browser to open the URL
This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/c-calls-the-browser-to-open-the-url/