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

发布于 2024-10-04 10:56:12 字数 341 浏览 5 评论 0原文

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

到目前为止,我已经尝试过进程监视器并搜索注册表,但我似乎找不到我的 Windows regedit 中的资源管理器收藏夹。

I am looking for a way to programmatically add a folder to the Favorites in Windows Explorer. Its Windows Explorer specific and based around this project: 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

信仰 2024-10-11 10:56:12

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

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

Instead of reading the registry, you can do the following:

string favoritesFolder = 
    Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
一人独醉 2024-10-11 10:56:12

PS:请务必查看@bsegraves'解决方案,我认为这是比我的好多了。

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

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(..) 方法自动扩展。 )

P.S.: Make sure to check out @bsegraves' solution, which I think is far better than mine.

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.)

清风夜微凉 2024-10-11 10:56:12

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

For Windows 8 this location has been changed to %USERPROFILE%\Links. Please refer to this answer.

把人绕傻吧 2024-10-11 10:56:12

从 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;

Starting from Vista FOLDERID_Links const was added. It points to Favorites of Windows explorer. My code (Delphi, but the main idea is visible):

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文