显然,微软已经(某种程度上)用快速访问项目替换了“收藏夹”Windows 资源管理器项目。但我还没有找到一种以编程方式向其中添加文件夹的方法(在 Google 和 MSDN 上都没有)。有没有办法做到这一点?
10 回答
在powershell中有一种简单的方法(至少):
$o = new-object -com shell.application
$o.Namespace('c:\My Folder').Self.InvokeVerb("pintohome")
希望能帮助到你。
Yohan Ney 对固定项目的回答是正确的。要取消固定项目,您可以执行以下操作:
$QuickAccess = New-Object -ComObject shell.application
($QuickAccess.Namespace("shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}").Items() | where {$_.Path -eq "C:\Temp"}).InvokeVerb("unpinfromhome")
这是我编写的一个脚本,以使 pin/unpin 更容易一些:
https://gallery.technet.microsoft.com/Set-QuickAccess-117e9a89
在 MS 发布 API 之前,它可能会对某人有所帮助。我运行了 procmon,似乎涉及这些注册表项
固定到快速访问:
HKEY_CLASSES_ROOT\Folder\shell\pintohome
取消固定时:
HKEY_CLASSES_ROOT\PinnedFrequentPlace\shell\unpinfromhome\command
固定时也使用此资源:(EDIT1:再也找不到它了..)
AppData\Roaming\Microsoft\Windows\Recent\AutomaticDestinations\{SOME_SORT_OF_GUID}.automaticDestinations-ms
您可以尝试使用 7-zip 打开它,里面有几个适合目标的文件
EDIT2:我发现在“运行”中运行它会打开快速访问:
shell:::{679F85CB-0220-4080-B29B-5540CC05AAB6}
我喜欢约翰的回答,但我添加了一些内容,以免删除一些已经存在的项目。我不小心把很多东西钉在那里,我一定选择了 pin 文件夹或其他东西来快速访问。
$QuickAccess = New-Object -ComObject shell.application
$okItems = @("Desktop","Downloads","Documents","Pictures","iCloud Photos","iCloud Drive","PhpstormProjects","Wallpapers 5","Videos", "Schedules for testing")
($QuickAccess.Namespace("shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}").Items() | where {$_.name -notin $okItems}).InvokeVerb("unpinfromhome");
根据这篇文章中的信息和这篇文章中关于 shell32 的一些信息https://stackoverflow.com/a/19035049 ,我能够使用 shell32 让它在 C# 中工作
您需要添加对“Microsoft Shell 控件和自动化”的引用。
这将添加一个链接
Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shellAppType);
Shell32.Folder2 f = (Shell32.Folder2)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { "C:\\temp" });
f.Self.InvokeVerb("pintohome");
这将按名称删除链接
Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shellAppType);
Shell32.Folder2 f2 = (Shell32.Folder2)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { "shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}" });
Console.WriteLine("item count: " + f2.Items().Count);
foreach (FolderItem fi in f2.Items())
{
Console.WriteLine(fi.Name);
if (fi.Name == "temp")
{
((FolderItem)fi).InvokeVerb("unpinfromhome");
}
}
编辑:经过进一步调查,我意识到快速访问包含两个“部分”。一个是固定项目,另一个是常用文件夹。出于某种原因,Music
默认Videos
情况下出现在第二部分(至少在 1909 年),与其他部分(桌面/下载/文档/图片)不同。所以调用的动词从unpinfromhome
to变化removefromhome
(定义在HKEY_CLASSES_ROOT\FrequentPlace
, CLSID:中{b918dbc4-162c-43e5-85bf-19059a776e9e}
)。在 PowerShell 中:
$Unpin = @("$env:USERPROFILE\Videos","$env:USERPROFILE\Music")
$qa = New-Object -ComObject shell.application
$ob = $qa.Namespace('shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}').Items() | ? {$_.Path -in $Unpin}
$ob.InvokeVerb('removefromhome')
在 Windows 1909 中,您无法使用建议的 PowerShell 解决方案从快速访问中取消固定Music
或链接。Videos
看起来它们很特别,因为它们不包含“pin”图标,不像其他的。
解决方案是固定和取消固定它们。我对 Windows API 或 PowerShell 了解不多,因此可能有一种不那么复杂的方式。
$Unpin = @("$env:USERPROFILE\Videos","$env:USERPROFILE\Music")
$qa = New-Object -ComObject shell.application
ForEach ($dir in $Unpin) { $qa.Namespace($dir).Self.InvokeVerb('pintohome') }
$ob = $qa.Namespace('shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}').Items() | ? {$_.Path -in $Unpin}
$ob.InvokeVerb('unpinfromhome')
另一种方法是重命名f01b4d95cf55d32a.automaticDestinations-ms
,然后注销/重新启动以重新创建它。但是不知道有没有副作用。批处理脚本:
:: f01b4d95cf55d32a => Frequent Folders
:: 5f7b5f1e01b83767 => Recent Files
rename "%APPDATA%\Microsoft\Windows\Recent\AutomaticDestinations\f01b4d95cf55d32a.automaticDestinations-ms" f01b4d95cf55d32a.automaticDestinations-ms.bak
void PinToHome(const std::wstring& folder)
{
ShellExecute(0, L"pintohome", folder.c_str(), L"", L"", SW_HIDE);
}
那是容易的部分,仍然无法从家里取消固定
基于其他人所说...这允许您删除所有固定文件夹(不仅仅是所有/最近的文件夹/项目):
$o = new-object -com shell.application
$($o.Namespace("shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}").Items() | where { $_.IsFolder -eq "True" -and ($($_.Verbs() | Where-Object {$_.Name -in "Unpin from Quick access"}) -ne $null)}).InvokeVerb("unpinfromhome")
我需要这个,所以我可以快速备份/恢复我的快速访问链接列表。所以我把它放在我的脚本的顶部(删除所有固定的项目,然后脚本的其余部分重新添加它们。这样可以确保顺序正确。
是的,我确信上面的代码有更好的语法。
对于使用 .NET Core 的用户:
遗憾的是,您不能在构建过程中包含对“Microsoft Shell 控件和自动化”的引用。
但是您可以改为使用动态,并省略参考:
public static void PinToQuickAccess(string folder)
{
// You need to include "Microsoft Shell Controls and Automation" reference
// Cannot include reference in .NET Core
System.Type shellAppType = System.Type.GetTypeFromProgID("Shell.Application");
object shell = System.Activator.CreateInstance(shellAppType);
// Shell32.Folder2 f = (Shell32.Folder2)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folder });
dynamic f = shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folder });
f.Self.InvokeVerb("pintohome");
}
并取消固定:
public static void UnpinFromQuickAccess(string folder)
{
// You need to include "Microsoft Shell Controls and Automation" reference
System.Type shellAppType = System.Type.GetTypeFromProgID("Shell.Application");
object shell = System.Activator.CreateInstance(shellAppType);
// Shell32.Folder2 f2 = (Shell32.Folder2)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { "shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}" });
dynamic f2 = shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { "shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}" });
foreach (dynamic fi in f2.Items())
{
if (string.Equals(fi.Path, folder))
{
fi.InvokeVerb("unpinfromhome");
}
}
}