【问题标题】:How to pin program to taskbar using C# in Windows 10如何在 Windows 10 中使用 C# 将程序固定到任务栏
【发布时间】:2020-06-05 00:36:04
【问题描述】:

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

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

【问题讨论】:

  • 为什么要控制用户体验?安装程序可以为用户提供在任务栏上固定内容的选项。
  • 当用户固定 squirrel 应用程序时遇到问题(部署解决方案后的那种点击)。用户固定特定版本,然后自动更新不起作用。

标签: c# windows-10 taskbar


【解决方案1】:

我看到了在 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");
    }
}

Pin program to taskbar using PS in Windows 10

【讨论】:

  • 它不适合我(Win10ent 1909),但我会继续努力。我可以从 Windows 资源管理器手动使用“{:}”contextMenu 并且它可以工作,但我的代码中的 InvokeVerb 似乎不起作用。
猜你喜欢
  • 2015-10-21
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 2010-12-22
  • 2016-06-06
  • 2012-03-09
  • 2011-10-15
  • 1970-01-01
相关资源
最近更新 更多