如何通过编程将应用程序固定到任务栏

时间:2019-04-24 07:39:06

标签: c# desktop-application

我已经尝试过此代码,它可以很好地将应用程序从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;
    }

1 个答案:

答案 0 :(得分:0)

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

Update KB3093266 removes shell.Application object 'taskbarpin' verb

  

更新KB3093266删除了shell.Application对象'taskbarpin'动词   用于添加任务栏图钉项目图钉

     

KB3093266的更新之一很可能破坏了它   取代


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