New here? Then you may want to subscribe to my rss feed. :)

How to change the default folder icon using NSIS

May 15th, 2007 | Posted in Development, NSIS | 2 Comments

NSIS InstallerNSIS (Nullsoft Scriptable Install System) is an open source installer with strong scripting capabilities.

A recent project required packaging up a RealBASIC (RB) application. When installing the RB application the folder it created for its own use under My Documents needed to have its own icon.

This was where, at long last, desktop.ini file came in handy.

Desktop.ini consists of three sections. The first two, [ExtShellFolderViews] and [{5984FFE0-28D4-11CF-AE66-08002B2E1262}] are if you want to use a custom Folder.htt template. Folder.htt allows you to create an alternative view of a web folder.

The third, [.ShellClassInfo] is where you format the look of the folder.

Lifted straight from MSDN:

ConfirmFileOp
Set this entry to 0 to avoid a “You Are Deleting a System Folder” warning when deleting or moving the folder.

NoSharing
Set this entry to 1 to prevent the folder from being shared.

IconFile
If you want to specify a custom icon for the folder, set this entry to the icon’s file name. The .ico file extension is preferred, but it is also possible to specify .bmp files, or .exe and .dll files that contain icons. If you use a relative path, the icon will be available to people who view the folder over the network. You must also set the IconIndex entry.

IconIndex
Set this entry to specify the index for a custom icon. If the file assigned to IconFile only contains a single icon, set IconIndex to 0.

InfoTip
Set this entry to an informational text string. It will be displayed as an infotip when the cursor hovers over the folder. If the user clicks the folder in a Web view, the information text will be displayed in the folder’s information block, below the standard information.

Replacing the folder icon involves setting the IconFile property to the actual .ico file like so:

  1. [.ShellClassInfo]
  2. ConfirmFileOp=0
  3. NoSharing=0
  4. IconFile=logo.ico
  5. IconIndex=0
  6. InfoTip=This is a tip

Your installer setup should now consist of your application, icon and destop.ini with IconFile set to the name of the icon.

The next step is to tell NSIS how to package them all up. (I have assumed that you have already a basic script ready for this to drop into). In your .nsi script add the following:

  1. ; Create the folder within My Documents
  2. CreateDirectory "$DOCUMENTS\My Folder"
  3. ; Tell where to output to next
  4. SetOutPath "$DOCUMENTS\My Folder"
  5. ; Extract both files
  6. File "logo.ico"
  7. File "desktop.ini"
  8.  
  9. ; Set the new folder as a system folder
  10. SetFileAttributes "$DOCUMENTS\My Folder" SYSTEM
  11. ; Set file attributes to system and hidden
  12. SetFileAttributes "$DOCUMENTS\My Folder\desktop.ini" HIDDEN|SYSTEM
  13. SetFileAttributes "$DOCUMENTS\My Folder\logo.ico" HIDDEN|SYSTEM

This creates the folder within My Documents, sets it to a system folder and extracts desktop.ini and logo.ico into the root of the new folder and sets them both as hidden system files.

Hopefully, you should now have the folder displaying your icon instead of the default folder icon. Any problems then ask.

Discuss this article »