ExtractAssociatedIcon返回null [英] ExtractAssociatedIcon returns null

查看:159
本文介绍了ExtractAssociatedIcon返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 ExtractAssociatedIcon 方法来检索文件的图标。我希望检索的用户将在他们的浏览器窗口中看到相同的图标。

 公共静态图标调用getIcon(字符串文件名) 
{

{
图标图标= Icon.ExtractAssociatedIcon(文件名);
返回图标;
}

{
返回NULL;
}
}

这工作的99%的时间。但是,如果用户链接到文件的共享路径,如 \\SOME_SERVER\my documents\this file.pdf 则返回null。它属于通过陷阱,该文件路径是不是有效的路径错误。



这是一个有效的URI(我已经验证该文件存在,是读等),但不与X有效的完全合格的驱动器路径:\some\folder符号



我怎样才能解决这个问题,如果在所有?



感谢。



重新更新



下面是我最终的解决方案。这比第一次更新更清洁。非常感谢克里斯·哈斯,他的答案是评论,而不是直接回答。如果/当他使它成为一个直接的答案,我会更新这个本身。



我仍然不得不下降到一个较低的水平,并通过C ++库提取图标,

 #地区的老派方法
函数[DllImport(SHELL32



:但我唯一需要的库如下。 DLL)]
静态外部的IntPtr ExtractAssociatedIcon(IntPtr的HINST,
StringBuilder的lpIconPath,出USHORT lpiIcon);

公共静态图标GetIconOldSchool(字符串文件名)
{
USHORT uicon;
StringBuilder的STRB =新的StringBuilder(文件名);
IntPtr的手柄= ExtractAssociatedIcon(IntPtr.Zero,STRB,出uicon);
ICO图标= Icon.FromHandle(句柄);

返回ICO;
}
#endregion

有一次,我所定义的上述方法,调用getIcon()方法变为:

 公共静态图标调用getIcon(字符串文件名) 
{

{
图标图标= Icon.ExtractAssociatedIcon(文件名);
返回图标;
}

{

{
图标图标2 = GetIconOldSchool(文件名);
返回图标2;
}

{
返回NULL;
}
}
}


解决方案


检查出的 -get最相关的图标从 - 一个网络共享文件>这里链接,最终导致的 p / Invoke.net 用下面的代码:

 函数[DllImport( SHELL32.DLL)] 
静态外部的IntPtr ExtractAssociatedIcon(IntPtr的HINST,StringBuilder的lpIconPath,出USHORT lpiIcon);

函数[DllImport(SHELL32.DLL)]
静态外部的IntPtr ExtractIcon(IntPtr的HINST,串lpszExeFileName,INT nIconIndex);



_

  USHORT uicon; 
StringBuilder的STRB =新的StringBuilder(YOUR_FILE_PATH);
IntPtr的手柄= ExtractAssociatedIcon(this.Handle,STRB,出uicon);
ICO图标= Icon.FromHandle(句柄);

返回ico.ToBitmap();


I'm using the ExtractAssociatedIcon method to retrieve the icon for the file. My hope is to retrieve the same icon that a user would see in their explorer window.

    public static Icon GetIcon(string fileName) 
    {
        try
        {
            Icon icon = Icon.ExtractAssociatedIcon(fileName);
            return icon;
        }
        catch
        {
            return null;
        }
    }

This works 99% of the time. However, if the user has linked to a file on a shared path, such as \\SOME_SERVER\my documents\this file.pdf it returns null. It falls through the "catch" with the error that the file path is not a valid path.

It is a valid URI (I've verified the file exists, is readable, etc.), but not a valid fully-qualified drive path with the X:\some\folder notation.

How can I get around this, if at all?

Thanks.

Re-UPDATE

Here's the solution I ended up with. It's much cleaner than the first update. Many thanks to Chris Haas, whose answer was a comment, and not a direct answer. If/when he makes it a direct answer, I will update this as such.

I still had to go down to a lower level and fetch the icon through C++ libraries, but the only library I needed is listed below:

    #region Old-School method
    [DllImport("shell32.dll")]
    static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, 
       StringBuilder lpIconPath, out ushort lpiIcon);

    public static Icon GetIconOldSchool(string fileName)
    {
        ushort uicon;
        StringBuilder strB = new StringBuilder(fileName);
        IntPtr handle = ExtractAssociatedIcon(IntPtr.Zero, strB, out uicon);
        Icon ico = Icon.FromHandle(handle);

        return ico;
    }
    #endregion

Once I had defined the above method, the GetIcon() method becomes:

    public static Icon GetIcon(string fileName) 
    {
        try
        {
            Icon icon = Icon.ExtractAssociatedIcon(fileName);
            return icon;
        }
        catch
        {
            try
            {
                Icon icon2 = GetIconOldSchool(fileName);
                return icon2;
            }
            catch
            {
                return null;
            }
        }
    }

解决方案

(Comment turned into post - CTIP)

Check out the link here which eventually leads to P/Invoke.net with the following code:

[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath, out ushort lpiIcon);

[DllImport("shell32.dll")]
static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);

_

ushort uicon;
StringBuilder strB = new StringBuilder(YOUR_FILE_PATH);
IntPtr handle = ExtractAssociatedIcon(this.Handle, strB, out uicon);
Icon ico = Icon.FromHandle(handle);

return ico.ToBitmap();

这篇关于ExtractAssociatedIcon返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆