如何在C#中获取默认浏览器的图标?

时间:2013-05-31 18:06:58

标签: c# .net icons

我有一个按钮,上面写着“在浏览器中打开”。我想将其重命名为“打开”并在其旁边显示默认浏览器的图标。

如果默认浏览器是firefox,那么我需要在我的按钮中使用firefox图标。 如果默认浏览器是chrome,那么我想要chrome图标。

如何获取默认浏览器图标?

如果每个版本的Windows都不同,那么我需要使用Windows 7版本。

2 个答案:

答案 0 :(得分:3)

在您的应用程序中嵌入一个空白的htm或html文件(或创建它)

然后在此文件上调用Icon.ExtractAssociatedIcon方法。

它将返回默认浏览器图标。

答案 1 :(得分:0)

我已经更改了一些代码,这是我的版本。

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();
        }
    }

}
}

以下是我使用代码的方式:

        DefaultSystemBrowser.Determine();
        if ( DefaultSystemBrowser.ErrorMessage == null )
        {
            btnOpenInBrowser.Image = DefaultSystemBrowser.Bitmap;
        }
        else
        {
            btnOpenInBrowser.Image = Properties.Resources.firefox_24_noshadow;
        }