How to find the location of Windows special folders.

By: mykle hoban

Abstract: This article describes how to find the physical location of folders such as the recycle bin and My Documents.

Question

How do I find the location of special folders (such as the Desktop, Recycle bin, etc.)?

Answer

The location of these files can be found by using the SHGetSpecialFolderLocation API call. Below is an example of finding some of the most common special folders.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  //cb is a combo box, lb is a label
  int folder;
  switch (cb->ItemIndex)
  {
    case 0:
      folder = CSIDL_BITBUCKET;
      break;
    case 1:
      folder = CSIDL_CONTROLS;
      break;
    case 2:
      folder = CSIDL_DESKTOP;
      break;
    case 3:
      folder = CSIDL_DRIVES;
      break;
    case 4:
      folder = CSIDL_FONTS;
      break;
    case 5:
      folder = CSIDL_NETWORK;
      break;
    case 6:
      folder = CSIDL_PRINTERS;
      break;
    case 7:
      folder = CSIDL_PROGRAMS;
      break;
    case 8:
      folder = CSIDL_RECENT;
      break;
    case 9:
      folder = CSIDL_SENDTO;
      break;
    case 10:
      folder = CSIDL_STARTMENU;
      break;
    case 11:
      folder = CSIDL_STARTUP;
      break;
    case 12:
      folder = CSIDL_TEMPLATES;
      break;
    default:
      folder = -1;
      break;
  }
  if (folder >= 0)
  {
    LPITEMIDLIST pidl;
    if (SHGetSpecialFolderLocation(Handle,folder,&pidl) == NOERROR)
    {
      char buf[MAX_PATH];
      SHGetPathFromIDList(pidl,buf);
      lb->Caption = buf;
    }
  }
}


Server Response from: ETNASC04