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

I am looking for a way to programmatically add a folder to my Favorites in Windows Explorer. Its Windows Explorer is defined and based on this project: http://www.codeproject.com/Tips/132804/Open-folders-using-a-Run-Command

So far I have tried Process Monitor and searched in the registry, but I cannot find my Favorites in Windows Explorer in regedit .


Microsoft changed this in Windows 8, so I marked my question accordingly. See comments in the marked answer for Win8 etc ..
+2
c # windows-7 windows-shell
source share
4 answers

PS: Remember to check out @bsegraves' solution , which, in my opinion, is much better than mine.

I'm not sure if this is what you are looking for, but I think your 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 get 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 is only a matter of creating a link or document in the specified folder.

(Note that this key value may be something like %USERPROFILE%\Favorites ; the environment variable should automatically expand using the .GetValue(..) method called above.)

+4
source share

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

 string favoritesFolder = Environment.GetFolderPath(Environment.SpecialFolder.Favorites); 
+6
source share

In Windows 8, this location has been changed to% USERPROFILE% \ Links. See this answer .

+3
source share

Starting with Vista FOLDERID_Links const added. It points to the 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; 
0
source share

All Articles