Pin and Un-pin items to/from the Windows 7 taskbar
One of the things I wanted to do with Lyre (my Windows 7 taskbar-based MP3 player), was to
- Figure out if a given executable is pinned to the taskbar
- Un-pin it from the taskbar
- Pin it back to the taskbar
During my searches, I found this blog post that says programmatic access to pinning and un-pinning has been disabled for a similar reason. I did find a couple of posts here and here, showing how to use a small shell script to pin and un-pin items.
The idea is rather simple, really. The verbs “Pin to Tas&kbar” and “Unpin from Tas&kbar” (the ampersand is the shortcut, it is required to directly execute the verb) are available on a shell link object. Executing one or the other will pin/unpin the specified shortcut.
How do we find out if an item is pinned to the taskbar? For that, we need to look in “%appdata%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\”, where these pinned “shortcuts” live. If we find a particular executable’s shortcut there, that means it’s pinned.
Now that we know what we need to do, the doing is quite simple, really.
Tell if a shortcut is pinned to the taskbar
- Examine each shortcut in the user’s pinned items path.
- If the path (or filename?) matches the executable you’re looking for, return true.
- Otherwise return false.
Pin/Unpin items to/from the taskbar
- Create a temporary shortcut to the executable we want to pin/unpin
- Create and execute a shell script that runs the appropriate verb (”Pin to Tas&kbar” or “Unpin from Tas&kbar“)
- We’re done!
Attached is a single .cs file that contains a class called Windows7TaskbarExtensions.cs.