.net - 如何从网络共享文件中获取关联的图标

标签 .net winforms

我正在使用 Icon.ExtractAssociatedIcon 来获取用户在 openfiledialog 中选择的文件的图标。

问题是如果用户从网络共享中选择一个图标,那么 openfiledialog 的文件名属性是 UNC 格式,这会导致 ArgumentExceptionExtractAssocaitedIcon :

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

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

所以我的问题给出了一个指定为 \\server\share\filename 的文件,我如何获得图标?

注: .NET 2.0

最佳答案

Reflector 看着这个,它最终调用 ExtractAssociatedIconshell32.dll .

您是否尝试过绕过 BCL 通过 PInvoke 访问它?

示例代码(通过 PInvoke.Net ):

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

 // ... snip
    ushort uicon;
    StringBuilder strB = new StringBuilder(260); // Allocate MAX_PATH chars
    strB.Append(openFileDialog1.FileName);
    IntPtr handle = ExtractAssociatedIcon(IntPtr.Zero, strB, out uicon);
    Icon ico = Icon.FromHandle(handle);

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

关于.net - 如何从网络共享文件中获取关联的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1842226/

相关文章:

c# - 使用内联逗号拆分逗号分隔的 csv 文件

.net - 一个窗口中有多个Web浏览器 session /进程

c# - Net Core : Execute All Dependency Injection in Xunit Test for AppService, 存储库等

c# - Winforms 应用程序中的击键导致窗口意外关闭

c# - 使用自定义字体在标签中绘制文本时参数无效

c++ - 基于 C++ 表单的应用程序中的 ActiveX 控件

c# - 将控制台应用程序移植到 winforms C#,静态类型错误

c# - 为 C# 应用程序制作一个安装程序,并将 .NET Framework 安装程序包含到安装程序中

.net - WAS 和 AppFabric 有区别吗

.net - serilog 中 seq 的摄取端口