173

I want to make my WPF application open the default browser and go to a certain web page. How do I do that?

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150

9 Answers9

360

For desktop versions of .NET:

System.Diagnostics.Process.Start("http://www.webpage.com");

For .NET Core, the default for ProcessStartInfo.UseShellExecute has changed from true to false, and so you have to explicitly set it to true for this to work:

System.Diagnostics.Process.Start(new ProcessStartInfo
    {
        FileName = "http://www.webpage.com",
        UseShellExecute = true
    });

To further complicate matters, this property cannot be set to true for UWP apps (so none of these solutions are usable for UWP).

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Inisheer
  • 20,376
  • 9
  • 50
  • 82
  • 3
    I used this as well, but now it turns out this doesn't work with UAC. In my application I have this in the manifest When I run the app under Windows 8 (where you cannot disable UAC anymore), I get the following exception when opening a web page: Win32Exception (0x80004005): Class not registered at System.Diagnostics.Process.StartWithShellExecuteEx – lvmeijer Jul 13 '12 at 15:39
  • This effect won't happen when I change requireAdministrator to asInvoker. But then the app isn't elevated :-( – lvmeijer Jul 13 '12 at 15:46
65

Accepted answer no longer works on .NET Core 3. To make it work, use the following method:

var psi = new ProcessStartInfo
{
    FileName = url,
    UseShellExecute = true
};
Process.Start (psi);
Alexander Smirnov
  • 1,573
  • 12
  • 23
40

I've been using this line to launch the default browser:

System.Diagnostics.Process.Start("http://www.google.com"); 
ajma
  • 12,106
  • 12
  • 71
  • 90
27

While a good answer has been given (using Process.Start), it is safer to encapsulate it in a function that checks that the passed string is indeed a URI, to avoid accidentally starting random processes on the machine.

public static bool IsValidUri(string uri)
{
    if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute))
        return false;
    Uri tmp;
    if (!Uri.TryCreate(uri, UriKind.Absolute, out tmp))
        return false;
    return tmp.Scheme == Uri.UriSchemeHttp || tmp.Scheme == Uri.UriSchemeHttps;
}

public static bool OpenUri(string uri) 
{
    if (!IsValidUri(uri))
        return false;
     System.Diagnostics.Process.Start(uri);
     return true;
}
cdiggins
  • 17,602
  • 7
  • 105
  • 102
5

Here is my complete code how to open.

there are 2 options:

  1. open using default browser (behavior is like opened inside the browser window)

  2. open through default command options (behavior is like you use "RUN.EXE" command)

  3. open through 'explorer' (behavior is like you wrote url inside your folder window url)

[optional suggestion] 4. use iexplore process location to open the required url

CODE:

internal static bool TryOpenUrl(string p_url)
    {
        // try use default browser [registry: HKEY_CURRENT_USER\Software\Classes\http\shell\open\command]
        try
        {
            string keyValue = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\http\shell\open\command", "", null) as string;
            if (string.IsNullOrEmpty(keyValue) == false)
            {
                string browserPath = keyValue.Replace("%1", p_url);
                System.Diagnostics.Process.Start(browserPath);
                return true;
            }
        }
        catch { }

        // try open browser as default command
        try
        {
            System.Diagnostics.Process.Start(p_url); //browserPath, argUrl);
            return true;
        }
        catch { }

        // try open through 'explorer.exe'
        try
        {
            string browserPath = GetWindowsPath("explorer.exe");
            string argUrl = "\"" + p_url + "\"";

            System.Diagnostics.Process.Start(browserPath, argUrl);
            return true;
        }
        catch { }

        // return false, all failed
        return false;
    }

and the Helper function:

internal static string GetWindowsPath(string p_fileName)
    {
        string path = null;
        string sysdir;

        for (int i = 0; i < 3; i++)
        {
            try
            {
                if (i == 0)
                {
                    path = Environment.GetEnvironmentVariable("SystemRoot");
                }
                else if (i == 1)
                {
                    path = Environment.GetEnvironmentVariable("windir");
                }
                else if (i == 2)
                {
                    sysdir = Environment.GetFolderPath(Environment.SpecialFolder.System);
                    path = System.IO.Directory.GetParent(sysdir).FullName;
                }

                if (path != null)
                {
                    path = System.IO.Path.Combine(path, p_fileName);
                    if (System.IO.File.Exists(path) == true)
                    {
                        return path;
                    }
                }
            }
            catch { }
        }

        // not found
        return null;
    }

Hope i helped.

Community
  • 1
  • 1
mr.baby123
  • 2,208
  • 23
  • 12
4

You cannot launch a web page from an elevated application. This will raise a 0x800004005 exception, probably because explorer.exe and the browser are running non-elevated.

To launch a web page from an elevated application in a non-elevated web browser, use the code made by Mike Feng. I tried to pass the URL to lpApplicationName but that didn't work. Also not when I use CreateProcessWithTokenW with lpApplicationName = "explorer.exe" (or iexplore.exe) and lpCommandLine = url.

The following workaround does work: Create a small EXE-project that has one task: Process.Start(url), use CreateProcessWithTokenW to run this .EXE. On my Windows 8 RC this works fine and opens the web page in Google Chrome.

lvmeijer
  • 1,022
  • 13
  • 14
  • 2
    See [comment](http://stackoverflow.com/questions/1173630/how-do-you-de-elevate-privileges-for-a-child-process#comment40205343_15041823), using `Explorer.exe` to de-elevate is not supported: "Unfortunately, the Windows Shell team has replied that the current behavior of "Explorer.exe AppName.exe" is a bug and may not work in future updates/versions of Windows. Applications should not rely upon it." – Carl Walsh Dec 07 '15 at 08:18
3

The old school way ;)

public static void openit(string x) {
   System.Diagnostics.Process.Start("cmd", "/C start" + " " + x); 
}

Use: openit("www.google.com");

Makyen
  • 31,849
  • 12
  • 86
  • 121
mohkirkuk
  • 127
  • 1
  • 3
2

I have the solution for this due to I have a similar problem today.

Supposed you want to open http://google.com from an app running with admin priviliges:

ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://www.google.com/");
Process.Start(startInfo); 
winsetter
  • 191
  • 1
  • 4
0
    string target= "http://www.google.com";
try
{
    System.Diagnostics.Process.Start(target);
}
catch (System.ComponentModel.Win32Exception noBrowser)
{
    if (noBrowser.ErrorCode==-2147467259)
    MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
    MessageBox.Show(other.Message);
}