How do I programmatically add a folder to the user's Favorites (in Windows Explorer)?

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

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

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


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


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;

Comments

  1. Mayson

    • 2017/2/22

    You can programmatically create a folder on your computer, create a //string fileName = "MyNewFile.txt"; // Use Combine again to add the 

  2. Mohammad

    • 2018/10/2

    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.

  3. Nasir

    • 2020/6/13

    In this article. Example; See also. This example creates a new folder in Microsoft Office Outlook. The name of the user who is logged on is 

  4. Williams

    • 2018/3/27

    You add both folders and files by pending add on them (and than checkin the changes in). If you pend add on the file then parent directory is added implicitly to SCC. Pending Add is done by Workspace.PendAdd

  5. Ira

    • 2016/3/1

    Box for their business, like programmatically creating default folder structures and automatically sharing content with new users.

  6. Braden

    • 2020/11/19

    When i try manually to create folder through UI with the same name , i get the message a folder with name already exists. Edit: I am able to add folder through UI and it is visible. The user credentials passed are of site collection administrator. I have tried in different browsers.

  7. Alvin

    • 2017/8/2

    This MATLAB function adds the specified folders to the top of the search path for the To modify the search path programmatically at startup, use addpath 

  8. Ambrose

    • 2020/5/28

    And I found how to use the AddItem method: var p = new Microsoft.Build.Evaluation.Project (@"C:\projects\MyProject.csproj"); p.AddItem ("Compile", @"C:\folder\file.cs"); p.Save (); The file will appear in project's root folder unless the project already had a folder called folder, in which case the file will be placed there.

  9. Jonas

    • 2016/9/22

    There are several ways to do this; one is: Use the execute command stage and enter the dos or Unix command to create your folders.

  10. Damian

    • 2021/2/15

    I need to give roles (including the Member role), memberdata, and user folders to certain OpenID users before they first log in to my Plone site. That is, I need to programmatically add OpenID users to my Plone site. portal_registration.addMember cannot add an OpenID user. plone.openid has its own PAS plugin which does not create member objects.

  11. Kendrick

    • 2019/1/14

    Edit: I am able to add folder through UI and it is visible. The user credentials passed are of site collection administrator. I have tried in different browsers 

  12. Conner

    • 2015/3/28

    Later on I need to delete the file but at that point, I am no longer impersonating the user that created the file and when an attempt is made to delete the file an exception is generated. My supposition is that at the time I create the file, I should be able to change the security attributes to allow any user to delete the file.

  13. Jayce

    • 2019/7/7

    Considering Folder is at one level, and not folders inside folders. This code will work. Also make sure, in list setting -> Advance Settings 

  14. Asa

    • 2015/7/3

    We have now set up a .NET application which allows our client to manage their users online, however they don’t have the knowledge or access to Management Studio in order to permit new users to access reports. Is there a way, through SP or .NET script to programmatically add users to the policy / security descriptor in SSRS.

  15. Jace

    • 2018/6/27

    a folder is a file with the MIME type application/vnd.google-apps.folder and with no extension. you can use the alias root to refer to the root 

  16. Royal

    • 2019/3/9

    This example creates a new folder in Microsoft Office Outlook. The name of the user who is logged on is used for the folder name. [!INCLUDEappliesto_olkallapp] 

Comments are closed.

Recent Posts