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:
Related Topics
C# 6.0 Features Not Working with Visual Studio 2015
How to Download a File from a Website in C#
List<T> Orderby Alphabetical Order
Redirect from Action Filter Attribute
C# List<String> to String with Delimiter
How to Quickly Check If Two Data Transfer Objects Have Equal Properties in C#
MVC Razor View Nested Foreach's Model
Differencebetween Directory.Enumeratefiles VS Directory.Getfiles
An Attempt Was Made to Access a Socket in a Way Forbidden by Its Access Permissions. Why
C# Image Resizing to Different Size While Preserving Aspect Ratio
Determine If a Sequence Contains All Elements of Another Sequence Using Linq
Does .Net Provide an Easy Way Convert Bytes to Kb, Mb, Gb, etc.
How to Get the Last Day of a Month
Generic Method Executed with a Runtime Type
Prevent Using Dispatcher.Invoke in Wpf Code
How to Retrieve Data Annotations from Code? (Programmatically)