如何从网络共享文件关联的图标图标、文件关联、网络

由网友(风再大也要抱住你)分享简介:我使用Icon.ExtractAssociatedIcon得到一个文件的图标,即用户选择,在打开文件对话框。 I am using Icon.ExtractAssociatedIcon to get the icon of a file , that a user selects, in an openfiledia...

我使用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 'serversharefilename' 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 serversharefilename, 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
阅读全文

相关推荐

最新文章