378

I am designing a small C# application and there is a web browser in it. I currently have all of my defaults on my computer say google chrome is my default browser, yet when I click a link in my application to open in a new window, it opens internet explorer. Is there any way to make these links open in the default browser instead? Or is there something wrong on my computer?

My problem is that I have a webbrowser in the application, so say you go to google and type in "stack overflow" and right click the first link and click "Open in new window" it opens in IE instead of Chrome. Is this something I have coded improperly, or is there a setting not correct on my computer

===EDIT===

This is really annoying. I am already aware that the browser is IE, but I had it working fine before. When I clicked a link it opened in chrome. I was using sharp develop to make the application at that time because I could not get c# express to start up. I did a fresh windows install and since I wasn't too far along in my application, I decided to start over, and now I am having this problem. That is why I am not sure if it is my computer or not. Why would IE start up the whole browser when a link is clicked rather than simply opening the new link in the default browser?

2
  • Hehe, convincing IE to open Chrome for you is going to be a bit of an uphill battle. Well, not a bit. This doesn't work either if you run IE directly. Or Chrome for that matter if IE is the default. Commented Jan 2, 2011 at 21:23
  • 1) getstartMenuDir Search For Firefox or Chrome StandartName Besure. if not found. 2) get list of standard install locations which ever exist 32 64 chrome ff use that. if not 3) last resort use the answers.
    – bh_earth0
    Commented Aug 30, 2018 at 9:34

21 Answers 21

625

On the .NET Framework (not on .Net Core or net50+), you can just write

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

EDIT: The WebBrowser control is an embedded copy of IE.
Therefore, any links inside of it will open in IE.

To change this behavior, you can handle the Navigating event.

15
  • 2
    Try it. Use Taskmgr.exe, you'll see two copies of iexporer.exe running. Navigating doesn't fire for the out-of-process one. Commented Jan 2, 2011 at 21:37
  • 5
    @Sean: Yes. Process.Start(e.Url.ToString())
    – SLaks
    Commented Jan 3, 2011 at 3:54
  • 3
    Local url (file:///) doesn't work with a querystring unless browser exe is specified as first param. Commented Sep 20, 2016 at 21:14
  • 7
    Be aware that this can method can also introduce a security issue, since, if the "url" is replaced with a physical path to an application it will also execute Commented Jul 18, 2017 at 6:46
  • 33
    Does not work with .NET Core. The accepted answer should support .NET Core, see Mayank Tripathi's answer below.
    – Andi
    Commented Sep 17, 2020 at 11:15
177

For those finding this question in dotnet core. I found a solution here

Code:

private void OpenUrl(string url)
{
    try
    {
        Process.Start(url);
    }
    catch
    {
        // hack because of this: https://github.com/dotnet/corefx/issues/10361
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            url = url.Replace("&", "^&");
            Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            Process.Start("xdg-open", url);
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
        {
            Process.Start("open", url);
        }
        else
        {
            throw;
        }
    }
}
13
  • 1
    I think not... my solution is kind an alternative.
    – equiman
    Commented Oct 27, 2017 at 13:03
  • 10
    For Windows, I recommend Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true }); Commented Apr 12, 2020 at 10:14
  • 2
    This should be the top answer, as it the original is outdated and is only still relevant on Windows. Commented Sep 7, 2020 at 5:54
  • 1
    Excellent answer Joel. Also worked for me on Windows and VS 2019 WPF(.NET Core) application. I would like to know how to open the URL on a defined browser. Specifically, open the URL only from Microsoft Edge. What changes shall I do in your code snippet?
    – NikSp
    Commented Oct 13, 2020 at 21:30
  • 1
    This will fail on Windows in some corporate environments where running cmd.exe is prevented.
    – user555
    Commented Feb 9, 2022 at 16:10
64

After researching a lot I feel most of the given answer will not work with dotnet core. 1.System.Diagnostics.Process.Start("http://google.com"); -- Will not work with dotnet core

2.It will work but it will block the new window opening in case default browser is chrome

 myProcess.StartInfo.UseShellExecute = true; 
    myProcess.StartInfo.FileName = "http://some.domain.tld/bla";
    myProcess.Start();

Below is the simplest and will work in all the scenarios.

Process.Start("explorer", url);
5
  • Works in Console app .Net Core 3.1
    – Kozbara
    Commented Sep 30, 2021 at 18:59
  • I also could not get System.Diagnostics.Process.Start("http://google.com") to work in WinUI 3. I was able to get this solution to work, however. Additionally, Windows.System.Launcher.LaunchUriAsync(new Uri("http://google.com")); works. Commented Jan 25, 2022 at 19:33
  • Actually this one will not work in all scenarios. It does not work with URLs with query strings, for instance. Try Process.Start("explorer", "https://google.com/search?q=C#"); and Windows will display your Documents folder. Commented Sep 25, 2023 at 9:26
  • @MustafaÖzçetin - Maybe query string needs to encode # as %23? "https://google.com/search?q=C%23". Commented Nov 1, 2023 at 22:35
  • @ToolmakerSteve: Tried yours but no difference. I tried with .NET Framework 4.7.2 and .NET 5 and both gives the same result. Commented Nov 2, 2023 at 5:54
44
public static void GoToSite(string url)
{
     System.Diagnostics.Process.Start(url);
}

that should solve your problem

2
  • 7
    should be 'static void GotoSite'
    – Behrooz
    Commented Jul 15, 2015 at 10:15
  • In my case it only works when I run the windows app with elevated permissions. I am using excel WPF application on windows 10 with Chrome as the default browser. Any idea how this can be fixed so I don't have to run with elevated permissions?
    – Gagan
    Commented May 6, 2021 at 20:46
37

Did you try Processas mentioned here: http://msdn.microsoft.com/de-de/library/system.diagnostics.process.aspx?

You could use

Process myProcess = new Process();

try
{
    // true is the default, but it is important not to set it to false
    myProcess.StartInfo.UseShellExecute = true; 
    myProcess.StartInfo.FileName = "http://some.domain.tld/bla";
    myProcess.Start();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}
7
  • 1
    UseShellExecute defaults to true.
    – SLaks
    Commented Jan 2, 2011 at 20:20
  • 4
    @SLaks, thanks. On the other hand it is important to mention that it has to be true.
    – Andreas
    Commented Jan 2, 2011 at 20:22
  • 1
    For those too lazy to check...Process requires "using System.Diagnostics" Commented Sep 27, 2016 at 15:27
  • 1
    This seems to be the route required by WPF. If you just do Process.Start(url), it doesn't open a new browser window. Commented Oct 16, 2019 at 13:45
  • 13
    Just to note - .NET Core no longer defaults UseShellExecute to true, so that line is required. Commented Nov 7, 2020 at 14:28
26

My default browser is Google Chrome and the accepted answer is giving the following error:

The system cannot find the file specified.

I solved the problem and managed to open an URL with the default browser by using this code:

System.Diagnostics.Process.Start("explorer.exe", "http://google.com");
1
  • On Windows that is the correct answer, cause without the "explorer.exe" argument Windows allways asks which of the installed browsers it should use. Commented Feb 15 at 12:02
23

I'm using this in .NET 5, on Windows, with Windows Forms. It works even with other default browsers (such as Firefox):

Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });

Based on this and this.

9

Am I the only one too scared to call System.Diagnostics.Process.Start() on a string I just read off the internet?

        public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
        {
            Request = request;
            string url = Request.Url;
            
            if (Request.TransitionType != TransitionType.LinkClicked)
            {   // We are only changing the behavoir when someone clicks on a link.
                // Let the embedded browser handle this request itself.
                return false;
            }
            else
            {   // The user clicked on a link.  Something like a filter icon, which links to the help for that filter.
                // We open a new window for that request.  This window cannot change.  It is running a JavaScript
                // application that is talking with the C# main program.
                Uri uri = new Uri(url);
                try
                {
                    switch (uri.Scheme)
                    {
                        case "http":
                        case "https":
                            {   // Stack overflow says that this next line is *the* way to open a URL in the
                                // default browser.  I don't trust it.  Seems like a potential security
                                // flaw to read a string from the network then run it from the shell.  This
                                // way I'm at least verifying that it is an http request and will start a
                                // browser.  The Uri object will also verify and sanitize the URL.
                                System.Diagnostics.Process.Start(uri.ToString());
                                break;
                            }
                        case "showdevtools":
                            {
                                WebBrowser.ShowDevTools();
                                break;
                            }
                    }
                }
                catch { }
                // Tell the browser to cancel the navigation.
                return true;
            }
        }

This code was designed to work with CefSharp, but should be easy to adapt.

8

Try this , old school way ;)

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

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

3
  • 4
    Can't this be exploited, ala, "Shellsock" ? Commented Feb 11, 2015 at 20:48
  • 1
    @JosephLennox that's an excellent point! it's probably worth mentioning that System.Diagnostics.Process.Start on the URL directly isn't much (any?) safer! on the other hand, if the user is running your application on THEIR computer (they probably are), the worst they can do is break their own system :P
    – Ben
    Commented Jun 5, 2015 at 21:25
  • 4
    @Ben Depends where the input is coming from. If it's a shared data source, once user could enter a malicious command and all other users who click "Go" would be at that user's mercy. Commented Mar 17, 2016 at 20:15
4

Take a look at the GeckoFX control.

GeckoFX is an open-source component which makes it easy to embed Mozilla Gecko (Firefox) into any .NET Windows Forms application. Written in clean, fully commented C#, GeckoFX is the perfect replacement for the default Internet Explorer-based WebBrowser control.

4
  • 1
    My problem is that I have a webbrowser in the application, so say you go to google and type in "stack overflow" and right click the first link and click "Open in new window" it opens in IE instead of Chrome. Is this something I have coded improperly, or is there a setting not correct on my computer
    – Sean
    Commented Jan 2, 2011 at 20:20
  • @SLaks: Why do you say that? I don't believe it is at all difficult to write create a string and set it equal to GetDefaultBrowserPath().
    – THE DOCTOR
    Commented Jan 2, 2011 at 20:24
  • @Sean: If you have a webbrowser within your application then it is going to use IE by default. The Navigating event can help you get around that.
    – THE DOCTOR
    Commented Jan 2, 2011 at 20:28
  • I have modified my answer after realizing that OP does not wish to change the default browser launched by a separate process.
    – THE DOCTOR
    Commented Jan 2, 2011 at 20:40
3

This opened the default for me:

System.Diagnostics.Process.Start(e.LinkText.ToString());
1
  • Is there a package that you are using that gives EventArgs the property of LinkText? Commented Sep 27, 2016 at 15:22
3

I tried

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

which works for most of the cases but I run into an issue having a url which points to a file:

The system cannot find the file specified.

So, I tried this solution, which is working with a little modification:

System.Diagnostics.Process.Start("explorer.exe", $"\"{uri}\"");

Without wrapping the url with "", the explorer opens your document folder.

4
  • 1
    So basically your answer is a comment or at least a duplicate
    – Vega
    Commented Jul 3, 2021 at 14:53
  • @Vega Sorry, I didn't have enough reputation to add a comment ... I added this answer because this modification is not mentioned in this thread. Commented Jul 4, 2021 at 10:20
  • 2
    This (finally) fixed the "cannot find the file" bug for me on .Net Core
    – gjvdkamp
    Commented Aug 9, 2021 at 12:34
  • 1
    The solution without the quotes worked for me until I had a URL with a query string, which opened the document folder. This solution works for query string URLs, too
    – h3n
    Commented Mar 8, 2023 at 20:41
3

Already best given answer imho:

For Windows, I recommend Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true }); – Matt Jenkins Apr 12, 2020 at 10:14

Updated to MS recommandation from 2023 found on https://learn.microsoft.com/en-us/troubleshoot/developer/visualstudio/csharp/language-compilers/start-internet-browser

System.Diagnostics.Process.Start(new ProcessStartInfo { FileName = "https://stackoverflow.com/", UseShellExecute = true });
1
2

In UWP:

await Launcher.LaunchUriAsync(new Uri("http://google.com"));
1
  • 1
    This is Launcher.LaunchUriAsync. Follow link for complete example. Interestingly, the return value allows caller to know if URL was opened or not. Beware, this is for Windows 8/Server2012/Phone8 and above. If the software has to be compatible with older versions, it can't use that. Commented Jan 6, 2017 at 13:06
2

dotnet core throws an error if we use Process.Start(URL). The following code will work in dotnet core. You can add any browser instead of Chrome.

var processes = Process.GetProcessesByName("Chrome");
var path  = processes.FirstOrDefault()?.MainModule?.FileName;
Process.Start(path,  url);
2
  • 6
    Not an answer to the Q, it is not known to the code what the default browser is
    – Arjan
    Commented Nov 26, 2019 at 14:43
  • 2
    It looks like the user needs to have Chrome already open for this to work.
    – beppe9000
    Commented May 17, 2021 at 12:40
1

to fix problem with Net 6 i used this code from ChromeLauncher ,default browser will be like it

internal static class ChromeLauncher
{
    private const string ChromeAppKey = @"\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe";

    private static string ChromeAppFileName
    {
        get
        {
            return (string) (Registry.GetValue("HKEY_LOCAL_MACHINE" + ChromeAppKey, "", null) ??
                                Registry.GetValue("HKEY_CURRENT_USER" + ChromeAppKey, "", null));
        }
    }

    public static void OpenLink(string url)
    {
        string chromeAppFileName = ChromeAppFileName;
        if (string.IsNullOrEmpty(chromeAppFileName))
        {
            throw new Exception("Could not find chrome.exe!");
        }
        Process.Start(chromeAppFileName, url);
    }
}
0

Open dynamically

string addres= "Print/" + Id + ".htm";
           System.Diagnostics.Process.Start(Path.Combine(Environment.CurrentDirectory, addres));
0

This works nicely for .NET 5 (Windows):

 ProcessStartInfo psi = new ProcessStartInfo {
   FileName = "cmd.exe",
     Arguments = $ "/C start https://stackoverflow.com/",
     WindowStyle = ProcessWindowStyle.Hidden,
     CreateNoWindow = true
 };
 Process.Start(psi);
1
  • 1
    This indeed works nicely, but not just for the user - it can be used to execute malicious code since you create an cmd.exe process and pass arguments to it Commented Aug 8, 2021 at 11:12
0

I'd comment on one of the above answers, but I don't yet have the rep.

System.Diagnostics.Process.Start("explorer", "stackoverflow.com");

nearly works, unless the url has a query-string, in which case this code just opens a file explorer window. The key does seem to be the UseShellExecute flag, as given in Alex Vang's answer above (modulo other comments about launching random strings in web browsers).

0

You can open a link in default browser using cmd command start <link>, this method works for every language that has a function to execute a system command on cmd.exe.

This is the method I use for .NET 6 to execute a system command with redirecting the output & input, also pretty sure it will work on .NET 5 with some modifications.

using System.Diagnostics.Process cmd = new();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();


cmd.StandardInput.WriteLine("start https://google.com"); 
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
-2

update the registry with current version of explorer
@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"

public enum BrowserEmulationVersion
{
    Default = 0,
    Version7 = 7000,
    Version8 = 8000,
    Version8Standards = 8888,
    Version9 = 9000,
    Version9Standards = 9999,
    Version10 = 10000,
    Version10Standards = 10001,
    Version11 = 11000,
    Version11Edge = 11001
}

key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.