如何以编程方式将应用程序固定到任务栏中 [英] How to pin application into taskbar programmatically

查看:80
本文介绍了如何以编程方式将应用程序固定到任务栏中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过此代码,它适用于在Windows 10中将应用程序从任务栏取消固定,但不适用于将应用程序固定到任务栏。

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

推荐答案

在您的解决方案中看不到任何错误,我也尝试了几次,但动词不再存在。经过一番研究,我发现了这个:

Update KB3093266 removes shell.Application object 'taskbarpin' verb

更新KB3093266删除外壳。应用程序对象‘taskbarping’谓词 用于添加任务栏管脚项目管脚

它很可能被KB3093266的某个更新破坏了 取代


和这个(PowerShell但相同的库):Pin to Taskbar fails in Windows 10

这篇关于如何以编程方式将应用程序固定到任务栏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆