Detecting the default browser…

On Oct 18, I wrote a article on showing hyperlinks from a WinForms 2.0 application. In that article, I also mentioned that I will be writing a article to check the default browser instead of hard coding the .exe file for Internet Explorer. Now this article discusses that technique.


private enum ShowCommands
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}

[DllImport(“shell32.dll”)]
static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);

IntPtr ptr = IntPtr.Zero;
ptr = ShellExecute(IntPtr.Zero, “open”, “http://channel9.msdn.com”, “”, “”, ShowCommands.SW_NORMAL);

What is happening here is ShellExecute Win32 API call is reading the registry entry at HKEY_CLASSES_ROOT\HTTP\shell\open\command. If you would like to delve under the hood on what is happening in the registry try Regmon. We can make this code more robust by checking whether the entry at that registry location is empty and ask the user to set the default browser in the registry which is pretty easy to do.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: