Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

vb.net - Icon.ExtractAssociatedIcon for things that are not files?

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:") did not work when I tried it, and threw an error.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...