繁体   English   中英

如何在 Windows 10 中使用 C# 将程序固定到任务栏

[英]How to pin program to taskbar using C# in Windows 10

如何使用 C# 将程序固定/取消固定到 Windows 10 中的任务栏?

适用于 Window 7 的解决方案并不总是适用于 Windows 10。

我见过适用于 Windows 10 的 PS 解决方案,这里有相同的解决方案转换为 C#

public static void PinToTaskBar(string filePath)
{
    if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);

    var valueData = Registry.LocalMachine.OpenSubKey(
        @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.taskbarpin");
    var key2 = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes\*", true);
    var key3 = key2.CreateSubKey("shell", true);
    var key4 = key3.CreateSubKey("{:}", true);
    key4.SetValue("ExplorerCommandHandler", valueData.GetValue("ExplorerCommandHandler"));

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

    // create the shell application object
    dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
    var directory = shellApplication.NameSpace(path);
    var item = directory.ParseName(fileName);
    item.InvokeVerb("{:}");

    key3.DeleteSubKey("{:}");
    if (key3.SubKeyCount == 0 && key3.ValueCount == 0)
    {
        key2.DeleteSubKey("shell");
    }
}

请参阅在 Windows 10 中使用 PS 将程序固定到任务栏

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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