问题描述
我有一个用 c# 编写的桌面应用程序,这个应用程序使用户能够在他们的机器硬盘驱动器上创建文件夹.在 Windows 7 和 8 上,应用程序会在 Windows 资源管理器窗口左侧的收藏夹菜单下为此文件夹创建快捷方式.
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 中没有收藏夹菜单,取而代之的是快速访问菜单,如果您右键单击文件夹,您可以选择固定文件夹以进行快速访问.
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 folderI 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%\Windows\Recent\AutomaticDestinations
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.
正如 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\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")
这最终使我找到了使用 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 的快速访问菜单中以编程方式固定\取消固定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!