简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM