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

4
我有一个按钮,上面写着“在浏览器中打开”。我想把它改成“打开”,并在旁边显示默认浏览器的图标。
如果默认浏览器是Firefox,则我想要Firefox图标显示在我的按钮上。 如果默认浏览器是Chrome,则我想要Chrome图标。
如何获取默认浏览器的图标?
如果每个Windows版本都不同,则我需要Windows 7版本的图标。

1
你尝试过什么? - It'sNotALie.
2
https://dev59.com/gGYr5IYBdhLWcg3wg6b9 - PiLHA
这篇stackoverflow的帖子将帮助你找到默认浏览器的Exe位置。而这篇stackoverflow的帖子将帮助你获取该exe的图标(也就是任何exe)。 - Yogee
newStackExchangeInstance - 我已经试过在谷歌上搜索了。 @Yogee - 谢谢 - 但我不会说 Visual Basic。 - Marko
@Yogee 请给OP一个真实的答案并标记为正确答案。 ;) - Vitor Canova
感谢Vitor的建议。我认为Marko可以从那些帖子中得到答案。这就是为什么我在评论中添加了我的答案,而不是将其放在答案中。@Marko:克服你的恐惧。VB代码是在.NET中编写的,所以不应该有太大的区别。只有一些小的语法使它与C#不同。.NET库是相同的。 - Yogee
2个回答

3

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

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

它会返回默认的浏览器图标。


我知道(火狐图标),但它在一张白纸上,右上角折叠了。为什么?我没有使用空白的HTML,我使用了我能找到的任何HTML。我想这没有什么区别。 - Marko
1
好的,我只测试了安装了Chrome而没有FireFox。这取决于浏览器是否安装了关联文件。似乎Chrome没有针对html/htm文件的特定图标。它返回与浏览器相同的图标。也许你应该尝试在浏览器exe本身上使用方法(ExtractAssociatedIcon)。这里是一个链接来找到它。https://dev59.com/gGYr5IYBdhLWcg3wg6b9 告诉我吧;-) - Chris

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

静态 DefaultSystemBrowser() { 确定(); } - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接