I am looking for a way to programmatically add a folder to the Favorit开发者_开发知识库es 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
.
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;
精彩评论