Get File Icon Used by Shell

Get File Icon used by Shell


Imports System.Drawing
Module Module1

Sub Main()
Dim filePath As String = "C:\myfile.exe"
Dim TheIcon As Icon = IconFromFilePath(filePath)

If TheIcon IsNot Nothing Then
''#Save it to disk, or do whatever you want with it.
Using stream As New System.IO.FileStream("c:\myfile.ico", IO.FileMode.CreateNew)
TheIcon.Save(stream)
End Using
End If
End Sub

Public Function IconFromFilePath(filePath As String) As Icon
Dim result As Icon = Nothing
Try
result = Icon.ExtractAssociatedIcon(filePath)
Catch ''# swallow and return nothing. You could supply a default Icon here as well
End Try
Return result
End Function
End Module

UWP get shell icon for a file

Well here's a helper metod which uses the dummy file approach (place it in some static class):

public async static Task<StorageItemThumbnail> GetFileIcon(this StorageFile file, uint size = 32)
{
StorageItemThumbnail iconTmb;
var imgExt = new[] { "bmp", "gif", "jpeg", "jpg", "png" }.FirstOrDefault(ext => file.Path.ToLower().EndsWith(ext));
if (imgExt != null)
{
var dummy = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("dummy." + imgExt, CreationCollisionOption.ReplaceExisting); //may overwrite existing
iconTmb = await dummy.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
}
else
{
iconTmb = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
}
return iconTmb;
}

Usage example:

var icon = await file.GetFileIcon();
var img = new BitmapImage();
img.SetSource(icon);

Get Shell Icon with only a file extension

Yes is possible using file extension as first parameter in SHGetFileInfo.
You must also use SHGFI_USEFILEATTRIBUTES flags.

How can I get large icons for a file extension using Windows shell?

The sizes for shell icons are given in the documentation for SHGetImageList:

  • 16x16 = small
  • 32x32 = large
  • 48x48 = extralarge
  • 256x256 = jumbo

So it is expected that when you ask for the "large" icons, you get 32x32. If you want the extralarge icon, you need to get them from the SHIL_EXTRALARGE image list.

Fastest way to get shell icon

Try using the SHGFI_USEFILEATTRIBUTES flag as well. See the articles Tuning SHGetFileInfo for Optimum Performance and What does SHGFI_USEFILEATTRIBUTES mean? for more information.

How to get the shell folder icon location for a specific folder?

There may not be a file path to an icon with the way Windows works. You mention "especially in thumbnail view", which means images and icons (within the folder) are stacked together in the folder icon. This does not save a file anywhere, so you can't load from any file.

I'm assuming this is the type of icon you're talking about:

Sample Image



Related Topics



Leave a reply



Submit