Icon Handler shows black icons

325 views Asked by At

I am using Sharpshell to change my pdf and xlsx icons. Most of the times it works fine, but sometimes it shows black icons. Refreshing or restarting explorer has no effect on it.

Moreover, it works fine in certain Icon Views - for example in Details, Content it works fine, but not in Medium Icons or Tiles. Also, the icons work fine in the Server Manager tool.

There is one more thing - if I change the name of the file that has got the black icon, the correct icon immediately appears.

This is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using SharpShell.SharpIconHandler;
using SharpShell.Attributes;
using SharpShell.Configuration;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using SharpShell.Diagnostics;
using SharpShell.Interop;

namespace IconHandlerTry2
{
    [ComVisible(true)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".pdf", ".xlsx")]
    public class Class1 : SharpIconHandler
    {
        /// <summary>
        /// Gets the icon.
        /// </summary>
        /// <param name="smallIcon">if set to <c>true</c> provide a small icon.</param>
        /// <param name="iconSize">Size of the icon.</param>
        /// <returns>
        /// The icon for the file.
        /// </returns>
        protected override Icon GetIcon(bool smallIcon, uint iconSize)
        {
            string prefix = "C:\\Windows\\IconsForYou\\";
            string filename = SelectedItemPath;
            string iconFileName;
            string[] splits = filename.Split('.');
            string extension = splits[splits.Length - 1];
            if (extension == "pdf")
                iconFileName = prefix + "pdfDefault.ico";
            else if (extension == "xlsx")
                iconFileName = prefix + "xlsxDefault.ico";
            else
                iconFileName = prefix + "default.ico";

            Icon icon = new Icon(iconFileName);
            //  Return the icon with the correct size. Use the SharpIconHandler 'GetIconSpecificSize'
            //  function to extract the icon of the required size.
            return GetIconSpecificSize(icon, new Size((int)iconSize, (int)iconSize));
        }
    }
}

This is the simpler version. I will be working on hundreds of icons. Does anybody have any suggestion?

EDIT: I now see that the icons are assigned randomly. Whenever I clear the icon cache, some random icons are displayed. Sometimes there is nothing, so it becomes either full black or white. Same thing is happening in the Server Manager Tool. Please help!

0

There are 0 answers