Browsing through Windows folders

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.

 

  

 uses ShlObj,ActiveX;

 

procedure TForm1.Button1Click(Sender: TObject);

var

  BI: TBrowseInfo;

  Buf: PChar;

  Dir,

  Root: PItemIDList;

  Alloc: IMalloc;

begin

  SHGetMalloc(Alloc);

  Buf := Alloc.Alloc(Max_Path);

 

  // CSIDL_BITBUCKET  RecycleBin

  // CSIDL_CONTROLS   ControlPanel

  // CSIDL_DESKTOP    Desktop

  // CSIDL_DRIVES     My Computer

  // CSIDL_FONTS      Fonts

  // CSIDL_NETHOOD    Network Neighborhood

  // CSIDL_NETWORK    The virtual version of the above

  // CSIDL_PERSONAL   'Personal'

  // CSIDL_PRINTERS   printers

  // CSIDL_PROGRAMS   Programs in the Start Menu

  // CSIDL_RECENT     Recent Documents

  // CSIDL_SENDTO     Folder SendTo

  // CSIDL_STARTMENU  The whole Start menu

  // CSIDL_STARTUP    The Autostart Group

  // CSIDL_TEMPLATES  Document templates

 

  // use of the constants above

  SHGetSpecialFolderLocation(Handle, CSIDL_PROGRAMS, Root);

 

  with BI do

  begin

    hwndOwner := Form1.Handle;

    // NIL means show all

    pidlRoot := Root;

    pszDisplayName := Buf;

    lpszTitle := 'Choose Folder';

    ulFlags := 0;

    lpfn := nil;

  end;

 

  try

    Dir := SHBrowseForFolder(BI);

    if Dir<>nil then

    begin

      SHGetPathFromIDList(Dir, Buf);

      ShowMessage(Buf);

      Alloc.Free(Dir);

    end;

  finally

    Alloc.Free(Root);

    Alloc.Free(Buf);

  end;

end;