I’m trying to write a C# application that can pin a shortcut to the taskbar in both Windows 10 and Windows 11.
I’ve found some information online, but I’m having trouble getting it to work consistently.
I found an api which helps in pinning current app to taskbar, but I want to pin its shortcut
I have also tried using powershell, but it’s not working
Here’s the code I’ve been using:
private static void PinShortcutToTaskbar(string shortcutPath)
{
// Path to the Taskbar's pinned items folder
string taskbarPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Microsoft\Internet Explorer\Quick Launch\User Pinned\Taskbar");
try
{
// Define the destination path
string destinationPath = Path.Combine(taskbarPath, Path.GetFileName(shortcutPath));
// Copy the shortcut to the taskbar pinned items folder
File.Copy(shortcutPath, destinationPath, true);
// Using Shell.Application to invoke the pin command
var shell = new Shell32.Shell();
var folder = shell.NameSpace(taskbarPath);
var folderItem = folder.ParseName(Path.GetFileName(shortcutPath));
// Invoke the pin command
folderItem.InvokeVerb("pintaskbar");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to pin shortcut: {ex.Message}");
}
}
I noticed how chrome is doing the same when we visit a website (goibibo) supporting web app: how it shows a dialog in window to add it in taskbar.
I tried code from following link:
https://learn.microsoft.com/en-us/answers/questions/1309489/powershell-commands-for-pinneditem-item-to-taskbar
/questions/31720595/pin-program-to-taskbar-using-ps-in-windows-10
/questions/74809556/win-11-pin-unpin-a-shortcut-programmatically-using-c-sharp
I want to achieve similar as attached in screenshot
enter image description here
Geetanshu Gulati is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4