Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically Pin\UnPin the folder from quick access menu in windows 10

I have a desktop application written in c#, and this application enables users to create the folder on their machine Hard drive . on windows 7 and 8, The App creates a shortcut for this folder under Favorit menu on the left side of windows Explorer window.

In windows 10 there is no Favorite menu, it was replaced by Quick access menu, and if you right click on the folder you can choose to Pin folder for quick access.

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

The problem is this exe does not contain an option for Unpin the folder from quick access so i will not be able to remove the shortcut from the quick access menu unless if I deleted it and I don't want to do that.

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

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

anyone can help in this ??

Do I need to know if there is any command that I can use it to Pin\Unpin folders to quick access from the command prompt?

like image 719
Laila Avatar asked Apr 20 '16 09:04

Laila


People also ask

How do I Pin and unpin a folder in quick access?

You can set a folder to show up in Quick access so it'll be easy to find. Just right-click it and select Pin to Quick access. Unpin it when you don't need it there anymore. If you want to see only your pinned folders, you can turn off recent files or frequent folders.

Why won't my folder unpin from quick access?

Here's a quick guide on how to do this: Open File Explorer and expand the Quick access menu. Next, select the pinned FTP folder and then hold the Ctrl key while selecting another pinned folder to do a multi-select. With both pinned folders selected, right-click on any of them.

How do I remove Pin from quick access from context menu?

Double-click on Pin to Quick access Context Menu - Add. reg or Pin to Quick access Context Menu - Remove. reg (Default). Click Yes when prompted by User Account Control.


1 Answers

I know it's a bit late, but I've found a way to do it and thought maybe someone could still use this.

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

I'm no expert in PowerShell, but I found a way to do it 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 the 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: For this to work, you need to add a reference to System.Management.Automation which can easily be obtained as a nuget.

like image 61
eitamal Avatar answered Jan 02 '23 11:01

eitamal