4

I need to be able to open a link in a browser from a C# application. Normally, I would use a code like this to open the link:

Process.Start(new ProcessStartInfo("explorer.exe", @"http://www.google.com"));

Unfortunately, that only succeeds in opening explorer and not a browser when the URL contains a query string such as: http://www.google.com/search?q=stackoverflow

How can I open URLs with query strings?

Edit Notes: I am using Windows 8 with non-IE default browsers. I am seeing the same error with 'Class Not Registered' when trying to use just Process.Start as described here: Process.Start(url) broken on Windows 8/Chrome - are there alternatives?

Cœur
  • 37,241
  • 25
  • 195
  • 267
cvocvo
  • 1,586
  • 2
  • 23
  • 38
  • What happens when you include a query string? – Ash Burlaczenko Jan 29 '13 at 14:54
  • I have a class not registered exception -- this situation going on: http://stackoverflow.com/questions/12206368/process-starturl-broken-on-windows-8-chrome-are-there-alternatives I AM running Windows 8 with Chrome as default browser. My code needs to work with ANY default browser. – cvocvo Jan 29 '13 at 15:04

4 Answers4

10

Finally found a solution -- kind of impressed I didn't try this to begin with after writing batch files years ago this was common.

Process.Start(new ProcessStartInfo("explorer.exe", "\"" + @"http://www.google.com/search?q=stackoverflow" + "\""));

Just adding quotes around it seems to work fine.

cvocvo
  • 1,586
  • 2
  • 23
  • 38
  • For anyone who uses this method in the code, I'd like to remind your guys of the limitation from the MAX_PATH of Windows API. If your URL is longer than 260 characters, it's likely to be cropped by the system. I have run into this issue when I try to handle OIDC authentication and I have to shrink the redirect_url and state to solve this. – John Cido Jan 05 '22 at 08:36
  • Thanks for this. Apparently unquoted it was using the last parameter which in my case was a redirecturl as the url and that was confusing. – John Lord Sep 14 '22 at 01:47
3

You can use the default browser with :

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

Simple isn't it?

And it works with query strings:

Process.Start(@"http://www.google.com/search?q=stackoverflow");
Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
  • Doing this gives me a "Class Not Registered" exception: at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)\r\n at System.Diagnostics.Process.Start()\r\n at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)\r\n at System.Diagnostics.Process.Start(String fileName)\r\n at QueryStringLaunch.Form1.button1_Click(Object sender, EventArgs e) in D:\\Documents\\Visual Studio 2010\\TestProjects\\QueryStringLaunch\\QueryStringLaunch\\Form1.cs:line 24" string – cvocvo Jan 29 '13 at 15:01
  • That class not registered error points to this issue: http://stackoverflow.com/questions/12206368/process-starturl-broken-on-windows-8-chrome-are-there-alternatives – cvocvo Jan 29 '13 at 15:02
  • Yes I am using Windows 8 x64 – cvocvo Jan 29 '13 at 15:05
  • 1
    Ideally that'd be the case but unfortunately over thousands of users I can't expect them to do this. – cvocvo Jan 29 '13 at 15:07
  • @cvocvo [Here](http://www.west-wind.com/weblog/posts/2012/Dec/12/ProcessStart-and-ShellExecute-fails-with-URLs-on-Windows-8) you may find the solution to register correctly your browser. Hope it helps. – Cédric Bignon Jan 29 '13 at 15:09
0

You just neet do do this:

try
{
   Process.Start(@"http://www.google.com/search?q=stackoverflow");
}
catch(Exception ex)
{
   MessageBox.Show(ex.ToString());
}
codeteq
  • 1,502
  • 7
  • 13
0

Try "start" instead of "explorer.exe". Pulling up the command line and typing < start http://google.com?q=blah > worked for me.

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62