Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I launch a URL in the users default browser from my application?

How can I have a button in my desktop application that causes the user's default browser to launch and display a URL supplied by the application's logic.

like image 243
Aaron Anodide Avatar asked May 08 '12 17:05

Aaron Anodide


People also ask

How do I find my default browser in C#?

The value of HKEY_CURRENT_USER\Software\Clients\StartMenuInternet should give you the default browser name for this user. You can use the name found in HKEY_CURRENT_USER to identify the default browser in HKEY_LOCAL_MACHINE and then find the path that way.

What is launching a URL?

Launch Url is the url you host your dotnet core application on and to which port the kestrel server is listening. Websettings, AppUrl is the url IIS is listening to. IIS (which is basically a reverse proxy here) will forward all the http request comming from the AppUrl to the Launch URL.


2 Answers

 Process.Start("http://www.google.com");
like image 186
Darin Dimitrov Avatar answered Oct 12 '22 02:10

Darin Dimitrov


Process.Start([your url]) is indeed the answer, in all but extremely niche cases. For completeness, however, I will mention that we ran into such a niche case a while back: if you're trying to open a "file:\" url (in our case, to show the local installed copy of our webhelp), in launching from the shell, the parameters to the url were thrown out.

Our rather hackish solution, which I don't recommend unless you encounter a problem with the "correct" solution, looked something like this:

In the click handler for the button:

string browserPath = GetBrowserPath();
if (browserPath == string.Empty)
    browserPath = "iexplore";
Process process = new Process();
process.StartInfo = new ProcessStartInfo(browserPath);
process.StartInfo.Arguments = "\"" + [whatever url you're trying to open] + "\"";
process.Start();

The ugly function that you shouldn't use unless Process.Start([your url]) doesn't do what you expect it's going to:

private static string GetBrowserPath()
{
    string browser = string.Empty;
    RegistryKey key = null;

    try
    {
        // try location of default browser path in XP
        key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

        // try location of default browser path in Vista
        if (key == null)
        {
            key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http", false); ;
        }

        if (key != null)
        {
            //trim off quotes
            browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
            if (!browser.EndsWith("exe"))
            {
                //get rid of everything after the ".exe"
                browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
            }

            key.Close();
        }
    }
    catch
    {
        return string.Empty;
    }

    return browser;
}
like image 33
neminem Avatar answered Oct 12 '22 01:10

neminem