186

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

9 Answers 9

380

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 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
This effect won't happen when I change requireAdministrator to asInvoker. But then the app isn't elevated :-(
70

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); 

Comments

41

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

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

Comments

28

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; } 

Comments

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.

Comments

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.

1 Comment

See comment, 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."
3

The old school way ;)

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

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

Comments

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); 

Comments

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); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.