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

    public int PriorityGetter()
    {
        return GetPriority();
    }

    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 true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    public bool CanShowOverlayGetter(string path)
    {
        return CanShowOverlay(path, FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL);
    }

    protected override System.Drawing.Icon GetOverlayIcon()
    {
        //  Return the read only icon
        return Properties.Resources.ReadOnly;
    }

    public System.Drawing.Icon OverlayIconGetter()
    {
        return GetOverlayIcon();
    }

   }
}

I am using the code above which I find from https://www.codeproject.com/Articles/545781/NET-Shell-Extensions-Shell-Icon-Overlay-Handlers

this is in a Class library and I reference this dll from my winform application when I need to overlayicon I am calling PriorityGetter then check with CanShowOverlayGetter finally call the OverlayIconGetter, If the CanShowOverlay function is returning true for a file it changes overlay icon but the problem is system uses this for every file in my computer without me doing anything when i register the dll and restart the explorer changes applies, but i want to check and change the icon overlay from a winform project that uploads and downloads files i want make icon overlay on those files that comes dynamically from the program. Do you guys have any idea how can i achieve this thanks!!!

Icarsel
  • 13
  • 6
  • [Icons and Icon Overlays](https://learn.microsoft.com/en-us/windows/win32/shell/icons-and-icon-overlays-bumper) -- [Creating Shell Extension Handlers](https://learn.microsoft.com/en-us/windows/win32/shell/handlers) – Jimi Sep 04 '20 at 10:34
  • @Jimi I don't really see how those articles will help me i already created the handler – Icarsel Sep 04 '20 at 10:38
  • Those docs contain step-by-step information on how to create and register Icon Overlay handlers, so you can verify, step by step, if your handler is behaving correctly. For example, I don't see a `GetOverlayInfo()` method there. That's something you should provide. `IsMemberOf()` is also not there either, but it may part of the library you mentioned. Verify that, too. etc. You also said that you see 12 already registered handlers. See whether there's a maximum number of potential Icon Overlay handlers. – Jimi Sep 04 '20 at 10:54
  • OverlayIconGetter returns the icon same as getoverlayinfo and memberof si basically canshowoverlay. There is 15 Icon Overlay handler if i am not mistaking but i don't know how to call those function etc. – Icarsel Sep 04 '20 at 11:07
  • When i played with the registers a little bit every file in my computers icon became what i had for my own iconoverlayerhandler but i need to only apply the change to specific file i am really confused with this issue – Icarsel Sep 04 '20 at 11:35
  • What determines whether the the Handler should provide an Overlay Icon is `IsMemberOf`. When called, it receives a Path to a Shell Object. By inspecting this object (the corresponding file extension, maybe), the Handler returns `S_OK` if it likes the object, `S_FALSE` if it doesn't or `E_FAIL` if something went wrong in the meanwhile. If `S_OK` is returned, the previously mapped Icon in system image list is used. The initialization call to `GetOverlayInfo()` determines the mapped Icon (which should be provided as a path to a.dll, .exe or .ico files). – Jimi Sep 04 '20 at 12:37
  • Okey the problem is i am using a servermanager it registers the dll and refreshes the explorer so as i understand it looks for every file in desktop but i want it to work for a file i choose in a winform how can i achieve this calling the dll from winform doesn't seem to work – Icarsel Sep 04 '20 at 13:20
  • Didn't you say that `CanShowOverlay()` *corresponds* to the original `IsMemberOf()` member of the Interface (maybe you're using an abstract class as bridge)? So, there you have `var fileAttributes = new FileInfo(path);`. Inspect the `FileInfo` and determine whether that's the File or File category you want to handle and return `true` or `false` accordingly. I don't know what a ServerManager is (a COM registration tool? No idea what's its role here). – Jimi Sep 04 '20 at 13:32
  • I implemented the code from link i posted. It's correspons to IsMemberOf() i suppose because for example when i used it for readonly files it only changes the icon for readonly files and when i change a files properity to read only it gets icon . But i want to use this icon overlay like dropbox i have sync project with winform i am checking when a file is created etc. with filesystem watcher and when something happens to file i want to change icon of it with this but i am sadly unable to achieve it. Sorry if i am bothering you i am trying everything but can't it for 3 days now – Icarsel Sep 04 '20 at 13:39
  • So, you're watching some files in a Folder, receive events for the changes you care about. How does this class, `SyncFileIconOverlay` know anything about it? Are you passing it a list of Files somewhere and you're not showing it here? Note that the FSW events are raised in ThreadPool Threads, unless you set the `SynchronizingObject` property to your Form. COM doesn't work very well in non STA threads (well, at all). BTW, you really shouldn't drop a `FileSystemWatcher` in the midst as it was nothing, it should be part of your question. You should post a question with these details instead. – Jimi Sep 04 '20 at 14:09

1 Answers1

0

You need to the some work before your icon overlay works because of the limit. You can check it here Making Icon Overlays Appear In Windows 7 and Windows 10.

...have a hard limit of 15 overlays. There’s a list in the registry, and no matter how many apps install overlays, only the first 15 are used. The rest are ignored.

Update 1
I couldn't find a better way to do it via file. But this sample will show overlay icon on specific folder.

Step 1: Set a file marker that can be hidden. In my sample i have .marker it's just a blank file.

Step 2: Your overlay icon handler.

[ComVisible(true)]
public class FileValidIconOverlayHandler : SharpIconOverlayHandler
{
    protected override int GetPriority()
    {
        return 10;
    }

    protected override bool CanShowOverlay(string path, FILE_ATTRIBUTE attributes)
    {
        var file = new FileInfo(path);
        var hasFileMarker = file.Directory.GetFiles(".marker").Length > 0;
        var isNotFileMarker = file.Name != ".marker";
        var isNotDirectory = !file.Attributes.HasFlag(FileAttributes.Directory);

        return hasFileMarker && isNotFileMarker && isNotDirectory;
    }

    protected override System.Drawing.Icon GetOverlayIcon()
    {
        return Properties.Resources.Valid;
    }
}

Output with .marker
enter image description here

Output without .marker
enter image description here

tontonsevilla
  • 2,649
  • 1
  • 11
  • 18
  • That is not the issue actually. I am trying to change icon overlay only when something happens to a file from my winform project. I am uploading file to a website for example, while this upload function is in process i want to icon overlay the file that is being uploaded to the system. But way my code work when i register the dll to system it checks for every folder file etc. in the computer i just want to change overlayicon for the file i want to change dynamically from my winform project – Icarsel Sep 05 '20 at 09:12
  • @Icarsel, can you share how you show your icon on your winform? – tontonsevilla Sep 05 '20 at 09:21
  • i am going to show icon changes on shell but only on specific files that are determined by winform application not gonna show on winform actually – Icarsel Sep 07 '20 at 06:20
  • @Icarsel, you can check my Update 1. – tontonsevilla Sep 08 '20 at 05:53