Icon.ExtractAssociatedIcon 不是文件的东西? [英] Icon.ExtractAssociatedIcon for things that are not files?

查看:17
本文介绍了Icon.ExtractAssociatedIcon 不是文件的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能吗?它给了我一个错误,我以前认为它也适用于文件夹和驱动器之类的东西.

Is this possible? It gives me an error, and I had previously thought it could work for folders and drives and stuff like that as well.

Icon.ExtractAssociatedIcon("C:\") 在我尝试时不起作用,并抛出错误.

Icon.ExtractAssociatedIcon("C:\") did not work when I tried it, and threw an error.

如何从一切中获取相关图标?这是vb.net

How can I get the associated icon from EVERYTHING? This is vb.net

推荐答案

SHGetFileInfo() shell 函数可以为您提供您正在寻找的图标.这段代码运行良好,它为驱动器、文件夹和文件生成了适当的图标:

The SHGetFileInfo() shell function can provide you with the icon you are looking for. This code worked well, it generated appropriate icons for drives, folders and files:

Imports System.Drawing
Imports System.Reflection
Imports System.Runtime.InteropServices

Public Module NativeMethods
  Public Function GetShellIcon(ByVal path As String) As Icon
    Dim info As SHFILEINFO = New SHFILEINFO()
    Dim retval as IntPtr = SHGetFileInfo(path, 0, info, Marshal.SizeOf(info), &H100)
    If retval = IntPtr.Zero Then Throw New ApplicationException("Could not retrieve icon")
    '' Invoke private Icon constructor so we do not have to copy the icon
    Dim cargt() As Type = { GetType(IntPtr) }
    Dim ci As ConstructorInfo = GetType(Icon).GetConstructor(BindingFlags.NonPublic Or BindingFlags.Instance, Nothing, cargt, Nothing)
    Dim cargs() As Object = { info.IconHandle }
    Dim icon As Icon = CType(ci.Invoke(cargs), Icon)
    Return icon
  End Function

  '' P/Invoke declaration
  Private Structure SHFILEINFO
    Public IconHandle As IntPtr
    Public IconIndex As Integer
    Public Attributes As Integer
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
    Public DisplayString As String
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
    Public TypeName As String
  End Structure

  Private Declare Auto Function SHGetFileInfo lib "Shell32.dll" (path As String, _
    attributes As Integer, byref info As SHFILEINFO, infoSize As Integer, flags As Integer) As IntPtr

End Module

这篇关于Icon.ExtractAssociatedIcon 不是文件的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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