简体   繁体   English

如何以编程方式将文件夹添加到用户的收藏夹(在Windows资源管理器中)?

[英]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资源管理器收藏夹。


Microsoft has changed this in Windows 8 so I have tagged my question accordingly. Microsoft已在Windows 8中对此进行了更改,因此我已相应地标记了我的问题。 Please see the comments in the marked answer for Win8 & etc details. 请参阅Win8及其他详细信息的标记答案中的注释。

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.

相关问题 如何获得Windows资源管理器收藏夹快捷方式(* .lnk)的列表? - How do I get a list of Windows Explorer Favorites shortcuts (*.lnk)? 如何在Windows资源管理器的“新建”上下文菜单中添加一个项目以打开一个窗口? - How do I add an item to Windows Explorer's “New” context menu which opens a window? 如何以编程方式在Windows 10资源管理器的导航窗格中创建指向文件夹的链接 - How to create link to folder in navigation pane of Windows 10 explorer programmatically 如何在Windows资源管理器中使用.NET或Windows API更改特定文件夹中的View列? (Windows 7及更高版本) - How do I use .NET or the Windows API to change the View columns in a particular folder in Windows Explorer? (Windows 7 and Up) 如何在Windows资源管理器中刷新文件的缩略图? - How do I refresh a file's thumbnail in Windows Explorer? 如何在 Windows 资源管理器中打开文件夹? - How can I open a folder in Windows Explorer? 在Windows Phone中添加到收藏夹 - Add to favorites in windows phone 如何打开USER Temp文件夹而不是Windows TEMP - How do I open USER Temp Folder, and not Windows TEMP 如何以编程方式使用Windows File Explorer执行搜索? - How can I programmatically use Windows File Explorer to perform a Search? 如何在Windows 10的“快速访问”部分添加文件夹? 或如何为文件夹调用Windows资源管理器上下文菜单选项? - How to add a folder to the “Quick Access” section in windows 10? OR How to call windows explorer context menu option for a folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM