File Extensions: Finding the default Icon

The Contingency

In case we can’t find the default icon we need to display the Windows icon. Here’s how:

No_Icon:
    'No icon could be found so we use the normal windows icon
    'This icon is held in shell32.dll in the system directory, Icon 0

    strDefaultIcon = Space(260)
    lngStringLength = GetSystemDirectory(strDefaultIcon, 260)
    strDefaultIcon = Left(strDefaultIcon, lngStringLength) & "SHELL32.DLL"
    lngIconNumber = 0
    GoTo Draw_Icon
    End Sub

The windows icon will be held in the file “SHELL32.DLL” which is in the system folder. So we get the system directory and use that (along with “SHELL32.DLL”) for the file name, then go back to “Draw_Icon”.

At this point I am just going to add an extra piece of code that will handle EXEs. This is because EXEs have no default icon but it’s probably not best to just show the windows icon. So lets use the icon that windows normally uses for EXEs:

Add this near the begging beneath “TempFileName = Right...” and above “lngError = RegOpenKey...”

    If LCase(TempFileName) = ".exe" Then
      strDefaultIcon = Space(260)
      lngStringLength = GetSystemDirectory(strDefaultIcon, 260)
      strDefaultIcon = Left(strDefaultIcon, lngStringLength) & "SHELL32.DLL"
      lngIconNumber = 2
      GoTo Draw_Icon
    End If

This code is just the same as for getting the windows icon, except we now want icon 2 instead of 0.

You might also like...

Comments

About the author

Phil Couling United Kingdom

Software developer, working in Derby, England. Currently I'm mostly developing in C++ with a combination of Borland, Visual Studio and MinGW.

Much of my work at the moment is with Databa...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson