-3

How to pin/unpin program to Task Bar in Windows 10 using C#?

Solutions that works for Window 7 are not always working for Windows 10.

Hacko
  • 1,551
  • 1
  • 15
  • 11
  • Why would you want to control the user experience? The installer can give the user the option of pin stuff the task bar. – Black Frog Feb 20 '20 at 16:15
  • We got problem when users pin squirrel application (kind of click once deploying solution). User pins specific version and then automatic updates do not work. – Hacko Feb 21 '20 at 10:12

1 Answers1

1

I have seen PS solution that works in Windows 10 and here is same solution translated to 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");
    }
}

See Pin program to taskbar using PS in Windows 10

Hacko
  • 1,551
  • 1
  • 15
  • 11
  • It's not working for me (Win10ent 1909) but I'll try to keep working at it. I am able to manually use the "{:}" contextMenu from Windows Explorer and it works, but the InvokeVerb from my code doesn't appear to work. – Thracx Sep 10 '21 at 18:22