4

I have a button that says "Open in browser". I would like to rename it to "Open" and show default browser's icon next to it.

If default browser is firefox, then I want firefox icon in my button. If default browser is chrome, then I want chrome icon.

How can I get the default browsers icon?

If it's different for every version of windows, then I need window 7 version.

Marko
  • 161
  • 2
  • 9
  • 1
    What have you tried? – It'sNotALie. May 31 '13 at 18:08
  • 2
    http://stackoverflow.com/questions/13621467/how-to-find-default-web-browser-using-c – PiLHA May 31 '13 at 18:08
  • [This](http://stackoverflow.com/questions/13621467/how-to-find-default-web-browser-using-c) post of stackoverflow will help you get default browser's Exe location. And [This](http://stackoverflow.com/questions/462270/get-file-icon-used-by-shell) post of stackoverflow will help you get icon of that exe (that means any exe). – Yogee May 31 '13 at 18:14
  • newStackExchangeInstance - I have tried googling @Yogee - thanks - but I don't speak Visual Basic – Marko May 31 '13 at 18:21
  • @Yogee Please make a real answer to OP mark accordingly. ;) – Vitor Canova May 31 '13 at 18:23
  • Thanks Vitor for your suggestion. I thought Marko can get answer from those threads. That's why I added my answer in comment instead of putting it in answer. @Marko: Conquer your fear. VB code is written in .NET so shouldn't make much difference. Only minor syntax make it different from C#. .NET Libraries are same. – Yogee Jun 03 '13 at 07:18

2 Answers2

3

Embed a blank htm or html file with your application (or create it)

Then call the Icon.ExtractAssociatedIcon method on this file.

It will return default browser icon .

Chris
  • 8,527
  • 10
  • 34
  • 51
  • I get it (firefox icon) but it's on some white piece of paper with top-right edge folded. Why? I didn't use blank html, I have used any html I could found. I guess that makes no difference. – Marko May 31 '13 at 18:48
  • 1
    OK, I have tested only with Chrome installed not FireFox. It depends if there is an association file installed with the browser. It seems that Chrome has no specific icon for html/htm file. it returns the same icon as the brower. Maybe you should try the method (ExtractAssociatedIcon ) on the browser exe itself. Here is a link to find it. http://stackoverflow.com/questions/13621467/how-to-find-default-web-browser-using-c Let me know ;-) – Chris May 31 '13 at 19:25
0

I have 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." );
    }

    /// <summary>
    /// Null if no error occured, error description otherwise.
    /// </summary>
    public static string ErrorMessage
    {
        get;
        private set;
    }

    /// <summary>
    /// Finds out all information about current default browser. You can call this method every time you want to find out default browser.
    /// </summary>
    public static void Determine()
    {
        path = String.Empty;
        initialized = true;

        RegistryKey regKey = null;
        ErrorMessage = null;

        try
        {
            //set the registry key we want to open
            regKey = Registry.ClassesRoot.OpenSubKey( "HTTP\\shell\\open\\command", false );

            //get rid of the enclosing quotes
            path = regKey.GetValue( null ).ToString().ToLower().Replace( "" + (char) 34, "" );

            //check to see if the value ends with .exe (this way we can remove any command line arguments)
            if ( !path.EndsWith( "exe" ) )
                //get rid of all command line arguments (anything after the .exe must go)
                path = path.Substring( 0, path.LastIndexOf( ".exe" ) + 4 );

            initialized = true;
        }
        catch ( Exception ex )
        {
            ErrorMessage = string.Format( "ERROR: An exception of type: {0} occurred in method: {1} in the following module: {2}", ex.GetType(), ex.TargetSite, typeof( DefaultSystemBrowser ) );
        }
        finally
        {
            //check and see if the key is still open, if so
            //then close it
            if ( regKey != null )
                regKey.Close();
        }
    }

}
}

Here is how I am using the code:

        DefaultSystemBrowser.Determine();
        if ( DefaultSystemBrowser.ErrorMessage == null )
        {
            btnOpenInBrowser.Image = DefaultSystemBrowser.Bitmap;
        }
        else
        {
            btnOpenInBrowser.Image = Properties.Resources.firefox_24_noshadow;
        }
Marko
  • 161
  • 2
  • 9