290

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?

Sean
  • 7,259
  • 14
  • 36
  • 46
  • 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. – Hans Passant Jan 02 '11 at 21:23
  • @SLaks Mono? What are you talking about? – Sean Jan 02 '11 at 22:38
  • 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 Aug 30 '18 at 09:34

14 Answers14

546

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.

jheriko
  • 2,928
  • 1
  • 19
  • 28
SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • Definitely the shortest variant. +good point mentioning the embedded IE problem. – Andreas Jan 02 '11 at 20:25
  • 1
    Try it. Use Taskmgr.exe, you'll see *two* copies of iexporer.exe running. Navigating doesn't fire for the out-of-process one. – Hans Passant Jan 02 '11 at 21:37
  • Huh? `Navigating` should fire for links in the WebBrowser. I never meant to say that you can control links in the new window; I meant to you can handle `Navigating` to suppress the new window and call `Process.Start`. – SLaks Jan 02 '11 at 21:39
  • What do you mean that I can suppress the new window and call a process start? Is there a way to get the url of the page that is supposed to open up before IE opens it, then tell the default browser to open it? – Sean Jan 03 '11 at 03:39
  • 5
    @Sean: Yes. `Process.Start(e.Url.ToString())` – SLaks Jan 03 '11 at 03:54
  • Is there any way you can get it to also bring the new browser window to the foreground? – Sam Harwell Jul 02 '14 at 13:34
  • you should mention the namespace. this function call can not be made in the way you suggest by default. – jheriko Jan 14 '15 at 14:21
  • @Slaks, is it possible to use the navigating event to prevent opening a new window? I've seen some examples on the web and I have not been able to get anything to work. Some help here is greatly appreciated. Cheers. – IbrarMumtaz Jan 22 '15 at 16:05
  • 2
    Local url (file:///) doesn't work with a querystring unless browser exe is specified as first param. – HerrimanCoder Sep 20 '16 at 21:14
  • 4
    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 – Gerrie Pretorius Jul 18 '17 at 06:46
  • @GerriePretorius, is there a more secure method to accomplish this? – Spencer Jan 28 '19 at 22:00
  • 1
    @Spencer: Make sure the URL is trusted. – SLaks Jan 29 '19 at 02:37
  • @Spencer as SLaks pointed out, just make sure you sanitize the URL to make sure it is actually a web address and not a physical path. – Gerrie Pretorius Jan 30 '19 at 13:11
  • 6
    Does not work with .NET Core. The accepted answer should support .NET Core, see Mayank Tripathi's answer below. – Andi Sep 17 '20 at 11: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 May 06 '21 at 20:40
79

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("cmd", $"/c start {url}") { CreateNoWindow = true });
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            Process.Start("xdg-open", url);
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
        {
            Process.Start("open", url);
        }
        else
        {
            throw;
        }
    }
}
Joel Harkes
  • 9,189
  • 2
  • 41
  • 58
  • Thanks! works for me ... but in macos with `open` command instead of `xdg-open` – equiman Oct 26 '17 at 15:32
  • @Equiman doesnt it do that? `RuntimeInformation.IsOSPlatform(OSPlatform.OSX)` or will it already be true at Linux? – Joel Harkes Oct 26 '17 at 15:34
  • I did it. But `xdg-command` returns "xdg-open: command not found". Then I tested with `open` command and works. – equiman Oct 26 '17 at 16:03
  • @Equiman so you are saying i should switch elseif linux and OSX around to make it work or does macos not fall udner OSX? – Joel Harkes Oct 27 '17 at 06:50
  • 1
    I think not... my solution is kind an alternative. – equiman Oct 27 '17 at 13:03
  • For Windows, I recommend `Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });` – Matt Jenkins Apr 12 '20 at 10:14
  • Wonderful solution. Ignore the m0ngoloids above. – jakob_a Aug 08 '20 at 16:27
  • This should be the top answer, as it the original is outdated and is only still relevant on Windows. – ForeverZer0 Sep 07 '20 at 05:54
  • 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 Oct 13 '20 at 21:30
38
public static void GoToSite(string url)
{
     System.Diagnostics.Process.Start(url);
}

that should solve your problem

yossico
  • 3,155
  • 3
  • 38
  • 69
user2193090
  • 405
  • 4
  • 2
  • 7
    should be 'static void GotoSite' – Behrooz Jul 15 '15 at 10:15
  • 6
    Not working in .NET Core / .NET 5.0 – HooNose Dec 14 '20 at 20:34
  • 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 May 06 '21 at 20:46
25

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);
}
Andreas
  • 3,589
  • 2
  • 23
  • 22
  • 1
    `UseShellExecute` defaults to true. – SLaks Jan 02 '11 at 20:20
  • 4
    @SLaks, thanks. On the other hand it is important to mention that it has to be `true`. – Andreas Jan 02 '11 at 20:22
  • 1
    For those too lazy to check...Process requires "using System.Diagnostics" – Nick Roberts Sep 27 '16 at 15:27
  • This seems to be the route required by WPF. If you just do `Process.Start(url)`, it doesn't open a new browser window. – Scott Salyer Oct 16 '19 at 13:45
  • 3
    Just to note - .NET Core no longer defaults `UseShellExecute` to true, so that line is required. – Mike Marynowski Nov 07 '20 at 14:28
  • 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 May 06 '21 at 20:41
18

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);
Mayank Tripathi
  • 353
  • 3
  • 9
9

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");
EstevaoLuis
  • 1,902
  • 7
  • 28
  • 35
6

Try this , old school way ;)

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

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

AbcAeffchen
  • 12,535
  • 15
  • 46
  • 62
mohkirkuk
  • 107
  • 1
  • 3
  • 2
    Can't this be exploited, ala, "Shellsock" ? – Joseph Lennox Feb 11 '15 at 20:48
  • @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 Jun 05 '15 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. – Dan Bechard Mar 17 '16 at 20:15
5

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.

THE DOCTOR
  • 4,095
  • 10
  • 40
  • 60
  • 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 Jan 02 '11 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 Jan 02 '11 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 Jan 02 '11 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 Jan 02 '11 at 20:40
4

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);
Natarajan Ganapathi
  • 389
  • 1
  • 6
  • 18
3

This opened the default for me:

System.Diagnostics.Process.Start(e.LinkText.ToString());
Xero Phane
  • 76
  • 7
2

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.

Alex Vang
  • 4,365
  • 3
  • 34
  • 54
1

In UWP:

await Launcher.LaunchUriAsync(new Uri("http://google.com"));
Kibernetik
  • 2,628
  • 25
  • 30
  • This is [Launcher.LaunchUriAsync](https://msdn.microsoft.com/library/windows/apps/windows.system.launcher.launchuriasync.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1). 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. – Stéphane Gourichon Jan 06 '17 at 13:06
0

Open dynamically

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

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);
dfgv
  • 101
  • 1
  • 10