I'm using a Windows Form application developed in .NET Framework 2.0. I can't upgrade the framework as 2.0 is the requirement. The application is used to host URL in WebBrowser control of a web application developed in .NET 4.5.2 and working fine. The web application loads HTML content which also contains the Hyperlinks. Now I am facing a the scenario where the IE11 is not available or it's disabled/uninstalled. When the windows form application runs and it loads the HTML content through the website and I try to click one of the hyperlink instead of opening the link in any other available browser (Chrome, Edge) it does nothing. Can you please give me a workaround to handle this scenario? I want the URL should be opened in any other available browser window.
Thanks.
CodePudding user response:
You can first get the link href of the url when click the link, then use Process.Start()
to launch Edge/Chrome to navigate to the link.
The sample code is like below, it launches Edge (msedge.exe) to open the links:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var linkElements = webBrowser1.Document.GetElementsByTagName("a");
foreach (HtmlElement link in linkElements)
{
link.Click = (s, args) =>
{
int iStartPos = link.OuterHtml.IndexOf("window.open('") ("window.open('").Length;
int iEndPos = link.OuterHtml.IndexOf("')", iStartPos);
String url = link.OuterHtml.Substring(iStartPos, iEndPos - iStartPos);
Process.Start("msedge.exe", url);
};
}
}