For launching a URL in the default browser, I used to write this simple c# code:
But during my tests today I got a nice exception when i execute this code:
System.ComponentModel.Win32Exception was unhandled HResult=-2147467259 Message=The system cannot find the file specified Source=System ErrorCode=-2147467259 NativeErrorCode=2 StackTrace: at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start(String fileName) at ConsoleApplication6.Program.Main(String args) in c:\Users\michaelseirer\Documents\Visual Studio 2013\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 20 ...
Now, it seems that this code in previous versions of Windows worked. And the reason, why it is not working on my system is simply because I have Chrome as my default browser... (the code works with Firefox as default browser)
Setting it back to IE solved the problem:
But: who wants this? ... ;)
So I googled a bit and found code, that reads the path to the current default browser in the Registry in HKEY_CLASSES_ROOT\http:
But - lo and behold! - the subtree does not change if you change to another default browser. This registry-place seems to be a dead end (it maybe was used in earlier versions of Windows?) This is also mentioned here in this thread at a Microsoft forum.
Starting with Windows Vista a new "Default Programs" was introduced. In this case, the value a user can set his own choice of browser (per user, not per machine!) - and it will be saved under the following registry key:
There, you can find a string that identifies the browser:
- FirefoxURL - for Firefox
- ChromeHTML - for Chrome
- IE.HTTP - for Internet Explorer
- and probably some more (but I have not tested others)
The value here is a pointer to HKEY_CLASSES_ROOT\<Name> , e.g. HKEY_CLASSES_ROOT\IE.HTTP. The concrete starting path to the executable can be found here: HKEY_CLASSES_ROOT\<name>\shell\open\command
This led to the following - though in my opinion huge amount of code - to actually launch a Url in the default browser:
This way, you open the URL in the browser the user actually specified (or that is configured system-wide - depending on the OS).
These have been the resources I found useful during my research and that helped me gathering the function above:
- How to retrieve default browsers path in C#
- Registry key for default browser (msdn forum)
After updating to the latest Google Chrome version (in my case I updated from 35.0.1916.114 to 35.0.1916.153) Process.Start worked also for Google Chrome. So it seems that only some versions of Chrome had this issue.