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

强颜欢笑 提交于 2019-12-04 20:17:30

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/36739317/programatically-pin-unpin-the-folder-from-quick-access-menu-in-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!