14

I'm writing a Windows service application in C# with FileSystemWatcher.

How can I add status icons to files and folders in Windows Explorer similar to how Dropbox or SVN do it?

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
spyke
  • 385
  • 3
  • 12

3 Answers3

17

You should develop an overlay icon handler and register it into the system.
Here you can find a partially working example written in C#.
Some MSDN documentation here and here.

Marco
  • 56,740
  • 14
  • 129
  • 152
  • I searched with Google all morning. It turned out that I just do not know how it correctly called. Thanks for the tip – spyke Dec 05 '11 at 10:20
  • 3
    I just found, that you must not write Shell Extension on .Net: [that's why](http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/1428326d-7950-42b4-ad94-8e962124043e/). But you can write it on C++ or something unmanaged and use it as a module in your .net program. – spyke Dec 05 '11 at 10:44
3

For anyone still interessted in this question:

Here is a codeproject link which describes the process in great detail.

It uses the SharpShell library, which can also be found on nuget.

Code, from the codeproject, looks something like that:

[ComVisible(true)]
public class ReadOnlyFileIconOverlayHandler : SharpIconOverlayHandler
{
  protected override int GetPriority()
  {
    //  The read only icon overlay is very low priority.
    return 90;
  }

  protected override bool CanShowOverlay(string path, FILE_ATTRIBUTE attributes)
  {
    try
    {
      //  Get the file attributes.
      var fileAttributes = new FileInfo(path);

      //  Return true if the file is read only, meaning we'll show the overlay.
      return fileAttributes.IsReadOnly;
    }
    catch (Exception) { return false; }
  }

  protected override System.Drawing.Icon GetOverlayIcon()
  {
    //  Return the read only icon.
    return Properties.Resources.ReadOnly;
  }
}
Firen
  • 272
  • 1
  • 13
  • Yup - I maintain the SharpShell library and this is a perfectly valid way to create a Shell Icon Overlay Handler. – Dave Kerr Oct 23 '18 at 14:33
2

I've never play with this, but I think it's the right way.

Custom Folder

First make the folder a System Folder, then create a Desktop.ini file and apply the change inside.

[.ShellClassInfo]
InfoTip=@Shell32.dll,-12690
IconFile=%SystemRoot%\system32\SHELL32.dll
IconIndex=-238  
2GDev
  • 2,478
  • 1
  • 20
  • 32