Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

453
Visualizações
Preload folder icon for a specific folder in Windows Icon cache, in C# or VB.NET

I need to mention a 3rd party program, or better said the source-code of WinThumbsPreloader program, which contains all the necessary code written in C# to preload the thumbnail of a file in Windows thumbnails cache as you can see in this demo:

enter image description here

The problem is that what I need to preload is the icon of a folder, but the IThumbnailCache::GetThumbnail method does not allow to pass a folder item, and it will return error code 0x8004B200 (WTS_E_FAILEDEXTRACTION):

The Shell item does not support thumbnail extraction. For example, .exe or .lnk items.

In other words, I need to do the same thing WinThumbsPreloader program does, but for folder icons instead of folder/icon thumbnails.

So, what I mean is that I have a folder with a desktop.ini file inside, which as you probably know it can serve to replace the default icon/thumbnail for the folder that stores that desktop.ini file. An example of desktop.ini file content:

[.ShellClassInfo]
IconResource=FolderPreview.ico,0

The reason why I need to preload folders is to avoid the icon cache generation every time that I navigate through a folder.

To clear doubts, I would like to avoid this slow folder icon generation:

before

And instead, get this improved speed:

after

My question is: In C# or VB.NET, how can I programmatically preload the icon of a specific folder in Windows icon cache?.

It seems that IThumbnailCache interface is not the way to go for this problem...


UPDATE 1

Following @Jimi's commentary suggestions, this is the approach I'm trying:

Public Shared Sub PreloadInIconCache(path As String, 
                                     Optional iconSize As Integer = 256)

    Dim iIdIShellItem As New Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")
    Dim shellItem As IShellItem = Nothing
    Dim shellItemIF As IShellItemImageFactory
    Dim iconFlags As IShellItemImageFactoryGetImageFlags = 
                     IShellItemImageFactoryGetImageFlags.IconOnly

    SHCreateItemFromParsingName(path, IntPtr.Zero, iIdIShellItem, shellItem)
    shellItemIF = DirectCast(shellItem, IShellItemImageFactory)
    shellItemIF.GetImage(New NativeSize(iconSize, iconSize), iconFlags, Nothing)

    Marshal.ReleaseComObject(shellItemIF)
    Marshal.ReleaseComObject(shellItem)
    shellItemIF = Nothing
    shellItem = Nothing

End Sub

It caches the icons. If I call this method for lets say a directory that contains 1000 subfolders with custom icons, then the size of iconcache_256.db increases like around 250 MB, so its a clear evidence that icons are being cached, however I'm doing something wrong because those cached icons are not used by the operating system. I mean, if after I call that method and then I manually use Explorer.exe to navigate to that directory, the operating system starts again extracting and caching the icons for all the 1000 subfolders creating NEW icon references, so iconcache_256.db doubles its file size to around 500 MB, and this is a evidence that iconcache_256.db contains both the icons I cached using the method above, and the icons cached by the operating system itself, so the icon cache references that I generate calling the method above differ in some way from the icon cache references generated by the operating system itself, and that is what I'm doing wrong...

What I'm doing wrong?.

UPDATE 2

Using WindowsAPICodePack v1.1 library (from Nuget manager) I'm having the same problem as described in the code of the UPDATE 1 using IShellItemImageFactory.GetImage: I can extract the icons and the icons are cached in iconcache_256.db file (and other *.db files too of less con cache size), but If I navigate to the directory through Explorer.exe then the operating system starts extracting and caching again the icon for the same folders I already cached...

Full code sample:

Imports Microsoft.WindowsAPICodePack.Shell
...

Dim directoryPath As String = "C:\Directory"
Dim searchPattern As String = "*"
Dim searchOption As SearchOption = SearchOption.AllDirectories

For Each dir As DirectoryInfo In New DirectoryInfo(directoryPath).EnumerateDirectories(searchPattern, searchOption)

    Console.WriteLine($"Extracting icon for directory: '{dir.FullName}'")

    Using folder As ShellFolder = DirectCast(ShellFolder.FromParsingName(dir.FullName), ShellFolder)
        folder.Thumbnail.FormatOption = ShellThumbnailFormatOption.IconOnly
        folder.Thumbnail.RetrievalOption = ShellThumbnailRetrievalOption.Default

        Using ico As Bitmap = folder.Thumbnail.Bitmap ' Or: folder.Thumbnail.Icon
            ' PictureBox1.Image = ico
            ' PictureBox1.Update()
            ' Thread.Sleep(50)
        End Using

    End Using

Next dir

I'm not sure why the operating system insists to extract new icons and cache them when I already cached them, this multiplies x2 the cached icon references in iconcache_256.db (and in the other *.db files), because if I iterate 1000 subfolders from a directory to extract and cache their icons, if after I do that I navigate to that folder through Explorer.exe then the O.S. will extract and cache again those 1.000 subfolder icons creating NEW entries in iconcache_256.db (and in the other *.db files).

I don't know how to read the database format of iconcache_256.db file, so I don't know the structure format, but if the structure takes a directory path as one of its fields then I suspect the code approaches I used maybe forces to add a directory path field that differs from what the operating system adds in the icon cache field when I navigate to the folder to cache icons through Explorer.exe, and maybe for that reason the icon cache references are multiplied x2... just I'm speculating...

UPDATE 3

I thought that maybe the real problem could be that the references added in icon cache *.db files maybe are per-process and for that reason when I navigate to the directory with Explorer.exe it starts extracting and caching again the icons that I already extracted and cached when debugging my executable in Visual Studio...

So I did a test running the same code to extract/cache folder icons from two different processes just by building the same project to two executable with different names and different assembly guids. I discovered that the icon cache references in *.db files are NOT multiplied/duplicated when I run the second executable, so the per-process idea is discarded.

I really don't know what more can I try... and what is differing in the "duplicated" references inside the *.db icon cache files.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda