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

时间:2010-11-24 21:36:56

标签: c# windows-7 windows-shell favorites

我正在寻找一种在Windows资源管理器中以编程方式将文件夹添加到收藏夹的方法。它特定于Windows资源管理器并基于此项目:http://www.codeproject.com/Tips/132804/Open-folders-using-a-Run-Command

到目前为止,我已经尝试过Process Monitor并搜索注册表,但我似乎无法在regedit中找到我的Windows资源管理器收藏夹。

<小时/> Microsoft已在Windows 8中对此进行了更改,因此我已相应地标记了我的问题。请参阅Win8&amp;标记答案中的注释。等细节。

4 个答案:

答案 0 :(得分:6)

您可以执行以下操作,而不是阅读注册表:

string favoritesFolder = 
    Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

答案 1 :(得分:4)

  

P.S。:请务必查看@bsegraves' solution,我认为这比我的要好得多。

我不确定这是否是您要找的,但我认为可以通过以下注册表值找到最喜欢的文件夹:

HKEY_CURRENT_USER\
  Software\
    Microsoft\
      Windows\
        CurrentVersion\
          Explorer\
            User Shell Folders\
              Favorites

您应该能够使用以下代码检索此文件夹名称:

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();

这只是在指定文件夹中创建链接或文档的问题。

(请注意,此键的值可能类似于%USERPROFILE%\Favorites;环境变量应自动通过上面调用的.GetValue(..)方法进行扩展。)

答案 2 :(得分:3)

对于Windows 8,此位置已更改为%USERPROFILE%\ Links。请参阅this answer

答案 3 :(得分:0)

从Vista FOLDERID_Links const开始添加。它指向Windows资源管理器的收藏夹。我的代码(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;