简体   繁体   English

以编程方式Pin \\ UnPin从Windows 10中的快速访问菜单中的文件夹

[英]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 . 我有一个用c#编写的桌面应用程序,这个应用程序使用户可以在他们的机器硬盘上创建该文件夹。 on windows 7 and 8, The App creates a shortcut for this folder under Favorit menu on the left side of windows Explorer window. 在Windows 7和8上,该应用程序在Windows资源管理器窗口左侧的“收藏夹”菜单下为该文件夹创建了一个快捷方式。

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. 在Windows 10中没有收藏菜单,它被快速访问菜单取代,如果您右键单击该文件夹,您可以选择固定文件夹以便快速访问。

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/ 为了以编程方式从c#代码中执行此操作,我找到了一个可以执行Pin操作的.exe,就好像用户单击菜单项来固定我从这里获取的文件夹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. 问题是这个exe不包含取消快速访问文件夹的选项,所以我将无法从快速访问菜单中删除快捷方式,除非我删除它并且我不想这样做。

I tried to find the shortcut file and I found it in this path %AppData%\\Windows\\Recent\\AutomaticDestinations 我试图找到快捷方式文件,我在这个路径%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? 我是否需要知道是否有任何命令可以将它用于Pin \\ Unpin文件夹以从命令提示符快速访问?

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. 正如Bradley Uffner所提到的,没有API可以避免不断滥用此类API。 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: 我不是PowerShell的专家,但我找到了一种使用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#: 这终于让我使用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 . 注意:为此,您需要添加对System.Management.Automation的引用,该引用可以很容易地作为nuget获取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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