如何从网络共享文件关联的图标 [英] How to get the associated icon from a network share file

查看:173
本文介绍了如何从网络共享文件关联的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Icon.ExtractAssociatedIcon得到一个文件的图标,即用户选择,在打开文件对话框。

I am using Icon.ExtractAssociatedIcon to get the icon of a file , that a user selects, in an openfiledialog.

现在的问题是,如果用户从网络共享上的图标,然后在打开文件对话框的文件名属性为UNC格式,这将导致一个的ArgumentException ExtractAssocaitedIcon

THe problem is if the user selects an icon from a network share then the filename property of the openfiledialog is in UNC format and this causes an ArgumentException in ExtractAssocaitedIcon:

Value of '\\server\share\filename' is not valid for 'filePath'.

Stack Trace:
   at System.Drawing.Icon.ExtractAssociatedIcon(String filePath, Int32 index)

我的问题是给指定为 \\服务器\共享\文件名的文件,我怎么图标?

So my question is given a file specified as \\server\share\filename, how do I get the icon?

注意: .NET 2.0

Note: .NET 2.0

推荐答案

望着这与反射,它最终调用 ExtractAssociatedIcon SHELL32.DLL

Looking at this with Reflector, it is ultimately calling ExtractAssociatedIcon in shell32.dll.

您是否尝试过周围的BCL会通过的PInvoke访问它?

Have you tried the going around the BCL accessing it via PInvoke?

样品code(通过 PInvoke.Net ):

Sample code (via PInvoke.Net):

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

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

    pictureBox1.Image = ico.ToBitmap();
 // ... snip

这篇关于如何从网络共享文件关联的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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