Author: William Gerbert
How to get special Windows folder location
Answer:
The two key functions to browse through those virtual Windows folders are
SHGetSpecialFolderLocation();
and
SHGetPathFromIDList()
They can be used as shown in the sample code. You may replace the CSIDL_PROGRAMS
constant with another one from the list in the comment below.
1 uses ShlObj, ActiveX;
2 3 procedure TForm1.Button1Click(Sender: TObject);
4 var5 BI: TBrowseInfo;
6 Buf: PChar;
7 Dir,
8 Root: PItemIDList;
9 Alloc: IMalloc;
10 begin11 SHGetMalloc(Alloc);
12 Buf := Alloc.Alloc(Max_Path);
13 14 // CSIDL_BITBUCKET RecycleBin15 // CSIDL_CONTROLS ControlPanel16 // CSIDL_DESKTOP Desktop17 // CSIDL_DRIVES My Computer18 // CSIDL_FONTS Fonts19 // CSIDL_NETHOOD Network Neighborhood20 // CSIDL_NETWORK The virtual version of the above21 // CSIDL_PERSONAL 'Personal'22 // CSIDL_PRINTERS printers23 // CSIDL_PROGRAMS Programs in the Start Menu24 // CSIDL_RECENT Recent Documents25 // CSIDL_SENDTO Folder SendTo26 // CSIDL_STARTMENU The whole Start menu27 // CSIDL_STARTUP The Autostart Group28 // CSIDL_TEMPLATES Document templates29 30 // use of the constants above31 SHGetSpecialFolderLocation(Handle, CSIDL_PROGRAMS, Root);
32 33 with BI do34 begin35 hwndOwner := Form1.Handle;
36 // NIL means show all37 pidlRoot := Root;
38 pszDisplayName := Buf;
39 lpszTitle := 'Choose Folder';
40 ulFlags := 0;
41 lpfn := nil;
42 end;
43 44 try45 Dir := SHBrowseForFolder(BI);
46 if Dir <> nilthen47 begin48 SHGetPathFromIDList(Dir, Buf);
49 ShowMessage(Buf);
50 Alloc.Free(Dir);
51 end;
52 finally53 Alloc.Free(Root);
54 Alloc.Free(Buf);
55 end;
56 end;