1 Attachment(s)
Extract the Icon Associated with any File on Your System
Contained in the zip file are two implementations, in both VB.NET and C#, of an "IconExtractor" class that allows you to obtain the icon associated with any file on your system. The implementation tucks away all the Win32 API stuff into a nice black box. You call the simple "Extract" method and pass it the file name and the desired icon size.
Re: Extract the Icon Associated with any File on Your System
Re: Extract the Icon Associated with any File on Your System
Thanks Man! I got a real kick out of your profile, by-the-way. :lol: Very funny stuff!
Re: Extract the Icon Associated with any File on Your System
This will crash if you send it a file that doesn't exist. To get the icon for "x.txt" where x.txt doesn't actually exists, add this to the top.
Code:
Private Const SHGFI_USEFILEATTRIBUTES = &H10
And add this to Extract()
Code:
Select Case Size
Case IconSize.Large
uflags = SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_USEFILEATTRIBUTES
Case Else
uflags = SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_USEFILEATTRIBUTES
End Select
For what I was doing, I actually needed to dynamically grab the icon for a folder. This won't crash, but it won't return the folder icon. The original code will. I got around this by sending Extract and optional parameter that was set to 1 by default.
Note: to use this don't make the above change to the Select Case
Code:
Public Overloads Function Extract(ByVal File As String, ByVal Size As IconSize, Optional ByVal UseFileAttributes As Integer = 1) As System.Drawing.Icon
.
.
.
'directly after the select case
If UseFileAttributes Then uflags = uflags Or SHGFI_USEFILEATTRIBUTES
EDIT:
WidgetMan, thanks a ton for this. It was very easy to use, and very helpful.