To bring up the extra icons set, first create a simple file folder on the Windows 10 desktop. Now all we need is a little test program.All major Windows operating systems, starting with Windows 95, allowed folder icon customization using the Shell32.DLL module. You can, of course, add further properties to return other icons – the only problem being thinking up a name for some of them! Trying it out As there is no logical reason why the user should even need to set these properties, we only define a get: static public Icon FloppyDisk Now we can add the properties to return the specific icons we are going to use. Notice the use of the FromHandle static method to create the icon object.
This is undocumented so if it you find it doesn’t work with another version of Windows (I used XP and Vista) you might have to implement the code needed to obtain the application handle.Īs well as using constants for the icon indexes, it also makes good programming sense to use a constant for the location of the Shell32.dll file: const string ShellIconsLib is worth adding a public method that will return the icon corresponding to any index: static public Icon GetIcon(int index) It is quite difficult to obtain the application instance handle and, after spending some time implementing other API calls to discover it, a programming error revealed that the ExtractIcon API call doesn’t actually seem to make use of it or need it.
The hInst parameter is supposed to be set to the application’s handle which is used by Windows to keep track of it. It returns a handle to the icon stored in the file given by the values of nIconIndex and lpszExeFileName. The API call is easy to translate to C#: [DllImport("shell32.dll", The following constants give the position of the device icons that we are specifically interested in, you can add more: private const int Floppy = 6 I don’t know of any documentation that gives the order of the icons in the file but it is fairly easy to discover what you are looking for by enumeration. They are stored in order with the first icon having index 0. We also need some constants to specify which icon. To make the class static, just add the modifier “static”: static class ShellIcons For example, to get the “hard disk” icon you would use something like: Icon icon=ShellIcons.HardDisk
The idea is that we are going to add static properties to the class that return each of the icons we want to use. A static class is automatically instantiated by the system and the resulting object has the same name as the class. There is no need to have more than one instance of the ShellIcons class so we can declare it to be static. As well as the standard classes we also need to add: using System.Drawing To make the shell icons look as much like the SystemIcon class as possible we need to create a new class within the project called ShellIcons. To pick an icon from the Shell32.dll file we have no real choice but to use the API call – ExtractIcon. There is an ExtractAssociatedIcon method in the Icon class that will extract a single icon from a file but it only returns the first icon found in the file. To do this you need to know that these are stored within the Shell32.dll file in the windows\system32 directory. This returns an Icon object for any of the icons that you will find in a message box or dialog box – but not the wider range of drive and folder icons. NET framework does provide some access to standard system icons in the SystemIcons class. If you spend time searching the documentation you probably won’t find out much about how you can use these icons but they aren’t deliberately hidden.
Windows uses a wide range of “standard” icons, the location of which seem to be well hidden from the average application programmer.