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

4

11

Even the programmatic pinning of icons in Windows 7 seems it's not permitted (like it says here: http://msdn.microsoft.com/en-us/library/dd378460(v=VS.85).aspx), there are some methods for doing this by using some VB scripts. Someone found a way of doing this in C# like this:

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

     // 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);
            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;
}

As can be seen, the code makes use of .NET Framework 4.0 features. The question I want to ask is: can this function be transformed so it would make the same thing, but using just 3.5 Framework? Any ideas?

Inconsistent answered 29/7, 2011 at 11:0 Comment(1)
I think this question should be moved to codereview.stackexchange.comLunarian
L
8

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.

Legwork answered 29/7, 2011 at 14:8 Comment(3)
But this is just so hackish. There has to be a better way!Protonema
@James, this seems to have changed for Windows 10. The verbName is just blank and it is not possible do invoke the 'DoIt' method. Could Microsoft have blocked it?Ainu
@Ainu Yes, Microsoft blocked it. But there are ways around that, see my answerArni
E
5

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;
            }
        }
Equilateral answered 19/11, 2015 at 11:56 Comment(0)
A
3

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.

Arni answered 10/8, 2016 at 14:21 Comment(0)
R
0

Update for UWP apps running on Windows 10 with the Fall Creators Update (Build 16299 or higher):

You can programmatically pin your own app to the taskbar, just like you can pin your app to the Start menu. And you can check whether your app is currently pinned, and whether the taskbar allows pinning.

You can access the TaskbarManager API directly, as described in MS docs.

I wanted to do this with a WinForms application, but it does not seem like it is going to work because I cannot reference the Windows.Foundation APIs.

Remediable answered 23/12, 2019 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.