The quickest way to add a custom folder icon in C# .NET

There are lots of long-winded methods out there that show you how to add / change a folders icon, below is a short C# class method that just gets the job done.

To change a folder icon we need to accomplish the following:

  • Ensure the target folder has the ‘System’ attribute applied to it
  • Add a desktop.ini file within the target folder specifying the icon path

The Code

public static void setFolderIcon(string path, string iconPath, string folderToolTip)
 {
 /* Remove any existing desktop.ini */
 if (File.Exists(path + @"desktop.ini")) File.Delete(path + @"desktop.ini");

/* Write the desktop.ini */
 StreamWriter sw = File.CreateText(path + @"desktop.ini");
 sw.WriteLine("[.ShellClassInfo]");
 sw.WriteLine("InfoTip=" + folderToolTip);
 sw.WriteLine("IconFile=" + iconPath);
 sw.WriteLine("IconIndex=0");
 sw.Close();
 sw.Dispose();

/* Set the desktop.ini to be hidden */
 File.SetAttributes(path + @"desktop.ini", File.GetAttributes(path + @"desktop.ini") | FileAttributes.Hidden);

/* Set the path to system */
 File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.System);
 }


2 thoughts on “The quickest way to add a custom folder icon in C# .NET”

  1. Raffi says:

    I think in line 9 you meant folderToolTip instead of filderToolTip. 😉
     
    Greets,
    Raffi

    1. olsgreen says:

      Indeed I did, thanks 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *