Pin *.lnk file to Windows 7 Taskbar using C#

Simple...

    private static void PinUnpinTaskBar(string filePath, bool pin) {
        if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);

        // create the shell application object
        Shell shellApplication = new ShellClass();

        string path = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(filePath);

        Folder directory = shellApplication.NameSpace(path);
        FolderItem link = directory.ParseName(fileName);

        FolderItemVerbs verbs = link.Verbs();
        for (int i = 0; i < verbs.Count; i++) {
            FolderItemVerb verb = verbs.Item(i);
            string verbName = verb.Name.Replace(@"&", string.Empty).ToLower();

            if ((pin && verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar"))) {

                verb.DoIt();
            }
        }

        shellApplication = null;
    }

Be sure to add a COM reference to "Microsoft Shell Controls And Automation".

If you want to keep the existing method of using Activator.CreateInstance so you don't have to have the extra COM interop DLL then you'll have to use reflection. But that would make the code a lot uglier.


regardless what localization the Windows user was using:

        int MAX_PATH = 255;
        var actionIndex = pin ? 5386 : 5387; // 5386 is the DLL index for"Pin to Tas&kbar", ref. http://www.win7dll.info/shell32_dll.html
        StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
        IntPtr hShell32 = LoadLibrary("Shell32.dll");
        LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
        string localizedVerb = szPinToStartLocalized.ToString();

        // create the shell application object
        dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));

        string path = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(filePath);

        dynamic directory = shellApplication.NameSpace(path);
        dynamic link = directory.ParseName(fileName);

        dynamic verbs = link.Verbs();
        for (int i = 0; i < verbs.Count(); i++)
        {
            dynamic verb = verbs.Item(i);

            if ((pin && verb.Name.Equals(localizedVerb)) || (!pin && verb.Name.Equals(localizedVerb)))
            {
                verb.DoIt();
                break;
            }
        }

In windows 10 the above methods don't work. The "Pin to Taskbar" verb doesn't appear in the list in your program, only in explorer. For this to work in windows 10, you have two options. Either rename you program to explorer.exe, or you have to fool the object in to thinking your program is called explorer.exe. You have to find the PEB and change the Image Path Field. I wrote a post here on how to do it.