简体   繁体   中英

How to pin application into taskbar programmatically

I have tried this code it works fine for unpinning application from taskbar in Windows 10 but it is not working for pinning application into taskbar.

public static void PinUnpinTaskbar(bool pin)
{
    string l_strFilePath = System.Reflection.Assembly.GetEntryAssembly().Location;
    if (!File.Exists(l_strFilePath)) throw new FileNotFoundException(l_strFilePath);
    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
                                         //uncomment the following line to pin to start instead
                                         //actionIndex = pin ? 51201 : 51394;
    StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH);
    IntPtr hShell32 = LoadLibrary("Shell32.dll");
    LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH);
    string localizedVerb = szPinToStartLocalized.ToString();

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

    // create the shell application object
    dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
    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 (verb.Name.Equals(localizedVerb))
        {
            verb.DoIt();
            return;
        }
    }
    return;
}

Cannot see anything wrong in your solution, also tried it several times but the verb does no longer exist. After some research I found this:

Update KB3093266 removes shell.Application object 'taskbarpin' verb

Update KB3093266 removes shell.Application object 'taskbarpin' verb for adding taskbar pin item pins

It is most likely broken by one of the updates that KB3093266 supersedes


And this (Powershell but same library): Pin to Taskbar fails in Windows 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM