我使用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
相关推荐
最新文章