Programmatically Pin \ UnPin Folder from Quick Access Menu in Windows 10

I have a desktop application written in C # and this application allows users to create a folder on their hard drive. on windows 7 and 8, the application creates a shortcut for this folder in the Favorites menu on the left side of the Window window.

In windows 10 there is no "Favorites" menu, it has been replaced by a quick access menu, and if you right-click on a folder, you can select the "Pin" folder for quick access.

To do this programmatically from within C # code, I found a .exe file that can perform the Pin action as if the user clicked on a menu item to link the folder I got it from here http://www.maddogsw.com/cmdutils/

The problem is that exe does not contain an option to unpin a folder from quick access, so I can’t remove the shortcut from the shortcut menu unless I delete it and I don’t want to do this.

I tried to find the shortcut file, and I found it on this path% AppData% \ Windows \ Recent \ AutomaticDestinations

but there is no mapping between this file label and the file itself. and at the same time, when I delete files from this path, all Pinned folders shortcuts remove not only my shortcut from quick access.

can anyone help with this?

Do I need to know if there is any command that I can use in the Pin \ Unpin folder for quick access from the command line?

+7
c # windows-10 winforms shortcuts
source share
1 answer

I know this a bit later, but I found a way to do it and thought maybe someone else could use it.

So, as Bradley Uffner mentioned, there is no API for this to avoid the constant abuse of such APIs. But there is still a (rather ugly) way to do this!

I am not an expert in PowerShell, but found a way to do this using PowerShell:

# To add 'C:\path\to\folder' to quick access: $qa = New-Object -ComObject shell.application $qa.NameSpace('C:\path\to\folder').Self.InvokeVerb("pintohome") # To remove 'C:\path\to\folder' from quick access: ($qa.Namespace("shell:::{679F85CB-0220-4080-B29B-5540CC05AAB6}").Items() | Where-Object { $_.Path -EQ 'C:\path\to\folder' }).InvokeVerb("unpinfromhome") 

Which finally led me to a solution using C #:

 using System.Management.Automation; using System.Management.Automation.Runspaces private static void AddFolderToQuickAccess(string pathToFolder) { using (var runspace = RunspaceFactory.CreateRunspace()) { runspace.Open(); var ps = PowerShell.Create(); var shellApplication = ps.AddCommand("New-Object").AddParameter("ComObject", "shell.application").Invoke(); dynamic nameSpace = shellApplication.FirstOrDefault()?.Methods["NameSpace"].Invoke(pathToFolder); nameSpace?.Self.InvokeVerb("pintohome"); } } private static void RemoveFolderFromQuickAccess(string pathToFolder) { using (var runspace = RunspaceFactory.CreateRunspace()) { runspace.Open(); var ps = PowerShell.Create(); var removeScript = $"((New-Object -ComObject shell.application).Namespace(\"shell:::{{679f85cb-0220-4080-b29b-5540cc05aab6}}\").Items() | Where-Object {{ $_.Path -EQ \"{pathToFolder}\" }}).InvokeVerb(\"unpinfromhome\")"; ps.AddScript(removeScript); ps.Invoke(); } } 

NOTE. To do this, you need to add a link to System.Management.Automation , which can be easily obtained as nuget .

+1
source share

All Articles