我有一个用 c# 编写的桌面应用程序,这个应用程序使用户能够在他们的机器硬盘上创建文件夹.在 windows 7 和 8 上,应用程序会在 windows Explorer 窗口左侧的收藏夹菜单下为此文件夹创建一个快捷方式.
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.
windows 10 中没有收藏夹菜单,取而代之的是快速访问菜单,如果你在文件夹上右击可以选择 Pin 文件夹进行快速访问.
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.
要从 c# 代码中以编程方式执行此操作,我发现了一个 .exe 可以执行 Pin 操作,就好像用户单击菜单项以固定文件夹一样我从这里得到它 http://www.maddogsw.com/cmdutils/
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/
问题是这个 exe 不包含从快速访问中取消固定文件夹的选项,所以我将无法从快速访问菜单中删除快捷方式,除非我删除它并且我不想这样做.
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.
我试图找到快捷方式文件,我在这个路径中找到它%AppData%WindowsRecentAutomaticDestinations
I tried to find the shortcut file and I found it in this path %AppData%WindowsRecentAutomaticDestinations
但此文件快捷方式与文件本身之间没有映射.同时,当我从此路径中删除文件时,所有固定文件夹快捷方式都从快速访问中删除,不仅是我的快捷方式.
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.
有人可以帮忙吗??
我是否需要知道是否有任何命令可以用于 PinUnpin 文件夹以从命令提示符快速访问?
Do I need to know if there is any command that I can use it to PinUnpin 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.
正如 Bradley Uffner 所提到的,没有用于避免此类 API 不断被滥用的 API.但是仍然有一种(相当丑陋的)方法可以做到!
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!
我不是 PowerShell 方面的专家,但我找到了一种使用 PowerShell 的方法:
I'm no expert in PowerShell, but I found a way to do it using PowerShell:
# To add 'C:path ofolder' to quick access:
$qa = New-Object -ComObject shell.application
$qa.NameSpace('C:path ofolder').Self.InvokeVerb("pintohome")
# To remove 'C:path ofolder' from quick access:
($qa.Namespace("shell:::{679F85CB-0220-4080-B29B-5540CC05AAB6}").Items() | Where-Object { $_.Path -EQ 'C:path ofolder' }).InvokeVerb("unpinfromhome")
这最终让我找到了使用 C# 的解决方案:
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();
}
}
注意:为此,您需要添加对 System.Management.Automation
的引用,该引用可以作为 nuget.
NOTE: For this to work, you need to add a reference to System.Management.Automation
which can easily be obtained as a nuget.
这篇关于从 Windows 10 的快速访问菜单中以编程方式 PinUnPin 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!