I changed the code a bit and here is my version.
using System; using Microsoft.Win32; using System.Drawing; namespace Namespace { public static class DefaultSystemBrowser { private static bool initialized = false; private static string path = null; public static string Path { get { CheckForErrors(); return path; } } public static Icon Icon { get { CheckForErrors(); return Icon.ExtractAssociatedIcon( path ); } } public static Bitmap Bitmap { get { CheckForErrors(); return Icon.ExtractAssociatedIcon( path ).ToBitmap(); } } private static void CheckForErrors() { if ( !initialized ) throw new InvalidOperationException( "You can't use DefaultSystemBrowser class before you call Determine()." ); if ( ErrorMessage != null ) throw new InvalidOperationException( "You can't use DefaultSystemBrowser class if call to Determine() resulted in error." ); }
This is how I use the code:
DefaultSystemBrowser.Determine(); if ( DefaultSystemBrowser.ErrorMessage == null ) { btnOpenInBrowser.Image = DefaultSystemBrowser.Bitmap; } else { btnOpenInBrowser.Image = Properties.Resources.firefox_24_noshadow; }
source share