[英]How do I programmatically add a folder to the user's Favorites (in Windows Explorer)?
I am looking for a way to programmatically add a folder to the Favorites in Windows Explorer. 我正在寻找一种方法,以编程方式将文件夹添加到Windows资源管理器中的收藏夹。 Its Windows Explorer specific and based around this project: http://www.codeproject.com/Tips/132804/Open-folders-using-a-Run-Command
它的Windows资源管理器特定,基于这个项目: http : //www.codeproject.com/Tips/132804/Open-folders-using-a-Run-Command
So far I've tried Process Monitor and searching the registry, but I can't seem to find my Windows Explorer Favourites in regedit
. 到目前为止,我已经尝试过Process Monitor并搜索注册表,但我似乎无法在
regedit
找到我的Windows资源管理器收藏夹。
Instead of reading the registry, you can do the following: 您可以执行以下操作,而不是阅读注册表:
string favoritesFolder =
Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
PS: Make sure to check out @bsegraves' solution , which I think is far better than mine.
PS:一定要查看@bsegraves的解决方案 ,我认为这比我的要好得多。
I'm not sure if this is what you're looking for, but I think the favorite folder can be found through the following registry value: 我不确定这是否是您正在寻找的,但我认为可以通过以下注册表值找到最喜欢的文件夹:
HKEY_CURRENT_USER\
Software\
Microsoft\
Windows\
CurrentVersion\
Explorer\
User Shell Folders\
Favorites
You should be able to retrieve this folder name with the following code: 您应该能够使用以下代码检索此文件夹名称:
using Microsoft.Win32;
...
RegistryKey topLevel = Registry.CurrentUser;
RegistryKey key = topLevel.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders",
true);
string favoriteFolder = key.GetValue("Favorites").ToString();
It's then only a matter of creating a link, or document, in the specified folder. 这只是在指定文件夹中创建链接或文档的问题。
(Take note that this key's value might be something like %USERPROFILE%\\Favorites
; the environment variable should automatically get expanded by the .GetValue(..)
method invoked above.) (请注意,此键的值可能类似于
%USERPROFILE%\\Favorites
;环境变量应自动通过上面调用的.GetValue(..)
方法进行扩展。)
For Windows 8 this location has been changed to %USERPROFILE%\\Links. 对于Windows 8,此位置已更改为%USERPROFILE%\\ Links。 Please refer to this answer .
请参考这个答案 。
Starting from Vista FOLDERID_Links const was added. 从Vista FOLDERID_Links const开始添加。 It points to Favorites of Windows explorer.
它指向Windows资源管理器的收藏夹。 My code (Delphi, but the main idea is visible):
我的代码(Delphi,但主要的想法是可见的):
procedure AddFileObjectToFavorites(AParent: HWND; const AObjectFileName: UnicodeString);
function GetFavorites: PItemIDList;
begin
if IsWindowsVistaOrLater then
OleCheck(SHGetKnownFolderIDList(FOLDERID_Links, 0, 0, Result))
else
OleCheck(SHGetFolderLocation(AParent, CSIDL_FAVORITES, 0, 0, Result));
end;
var
Desktop: IShellFolder;
Eaten: DWORD;
Attr: DWORD;
ObjectIDList: PItemIDList;
ObjectParentFolder: IShellFolder;
ObjectChildIDList: PItemIDList;
LinksIDList: PItemIDList;
LinksParentFolder: IShellFolder;
LinksChildIDList: PItemIDList;
DataObject: IDataObject;
LinksDropTarget: IDropTarget;
Effect: Integer;
begin
OleCheck(SHGetDesktopFolder(Desktop));
try
Attr := 0;
OleCheck(Desktop.ParseDisplayName(AParent, nil, PWideChar(AObjectFileName), Eaten, ObjectIDList, Attr));
try
SHBindToParent(ObjectIDList, IShellFolder, Pointer(ObjectParentFolder), ObjectChildIDList);
try
LinksIDList := GetFavorites;
try
OleCheck(SHBindToParent(LinksIDList, IShellFolder, Pointer(LinksParentFolder), LinksChildIDList));
try
OleCheck(LinksParentFolder.GetUIObjectOf(AParent, 1, LinksChildIDList, IDropTarget, nil, LinksDropTarget));
try
OleCheck(ObjectParentFolder.GetUIObjectOf(AParent, 1, ObjectChildIDList, IDataObject, nil, DataObject));
try
Effect := DROPEFFECT_LINK;
OleCheck(LinksDropTarget.DragEnter(DataObject, 0, Point(0, 0), Effect));
if Effect and DROPEFFECT_LINK = 0 then
begin
OleCheck(LinksDropTarget.DragLeave);
raise Exception.Create('Cannot drop');
end;
Effect := DROPEFFECT_LINK;
OleCheck(LinksDropTarget.Drop(DataObject, 0, Point(0, 0), Effect));
finally
DataObject := nil;
end;
finally
LinksDropTarget := nil;
end;
finally
LinksParentFolder := nil;
end;
finally
CoTaskMemFree(LinksIDList);
end;
finally
ObjectParentFolder := nil;
end;
finally
CoTaskMemFree(ObjectIDList);
end;
finally
Desktop := nil;
end;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.