How to get WINDOWS special directories path in Delphi?

Sometime we need some special directories path from Microsoft Windows system to store User data or to copy some files etc. So we can get those folder paths in Delphi in several ways. In this blog I have tried to cover all the ways. If I have left something please feel free to add comments.

By using Windows API function SHGetFolderPath 
Delphi provides SHGetFolderPath API function which helps us to get windows system folder paths. We need to use ShlObj unit in uses clause.

For more details about SHGetFolderPath please visit following link

Here at following I have created a string function GetSpecialFolderPath using SHGetFOlderPath API function which will return all special Windows folder path as per CSIDLFolder value.

function GetSpecialFolderPath(CSIDLFolder: Integer): string;
var
   FilePath: array [0..MAX_PATH] of char;
begin
  SHGetFolderPath(0, CSIDLFolder, 0, 0, FilePath);
  Result := FilePath;
end;
// to get DESKTOP folder location //
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(GetSpecialFolderPath(CSIDL_DESKTOP, False));
end;

CSIDL (constant special item ID list) values provide a unique system-independent way to identify special folders used frequently by applications.
Following CSIDL Constants we can use.

  CSIDL_DESKTOP                   = $0000; { <desktop> }
  CSIDL_INTERNET                  = $0001; { Internet Explorer (icon on desktop) }
  CSIDL_PROGRAMS                = $0002; { Start Menu\Programs }
  CSIDL_CONTROLS                 = $0003; { My Computer\Control Panel }
  CSIDL_PRINTERS                  = $0004; { My Computer\Printers }
  CSIDL_PERSONAL                 = $0005; { My Documents.  This is equivalent to 
                                                                       CSIDL_MYDOCUMENTS in XP and above }
  CSIDL_FAVORITES                 = $0006; { <user name>\Favorites }
  CSIDL_STARTUP                    = $0007; { Start Menu\Programs\Startup }
  CSIDL_RECENT                      = $0008; { <user name>\Recent }
  CSIDL_SENDTO                     = $0009; { <user name>\SendTo }
  CSIDL_BITBUCKET                 = $000a; { <desktop>\Recycle Bin }
  CSIDL_STARTMENU                = $000b; { <user name>\Start Menu }
  CSIDL_MYDOCUMENTS           = $000c; { logical "My Documents" desktop icon }
  CSIDL_MYMUSIC                    = $000d; { "My Music" folder }
  CSIDL_MYVIDEO                    = $000e; { "My Video" folder }
  CSIDL_DESKTOPDIRECTORY   = $0010; { <user name>\Desktop }
  CSIDL_DRIVES                       = $0011; { My Computer }
  CSIDL_NETWORK                   = $0012; { Network Neighborhood (My Network Places) }
  CSIDL_NETHOOD                   = $0013; { <user name>\nethood }
  CSIDL_FONTS                        = $0014; { windows\fonts }
  CSIDL_TEMPLATES                 = $0015; { <user name>\appdata\roaming\template folder }
  CSIDL_COMMON_STARTMENU = $0016; { All Users\Start Menu }
  CSIDL_COMMON_PROGRAMS  = $0017; { All Users\Start Menu\Programs }
  CSIDL_COMMON_STARTUP     = $0018; { All Users\Startup }
  CSIDL_COMMON_DESKTOPDIRECTORY  = $0019; { All Users\Desktop }
  CSIDL_APPDATA                    = $001a; { <user name>\Application Data }
  CSIDL_PRINTHOOD                = $001b; { <user name>\PrintHood }
  CSIDL_LOCAL_APPDATA         = $001c; { <user name>\Local Settings\Application Data 
                                                                                (non roaming) }
  CSIDL_ALTSTARTUP               = $001d; { non localized startup }
  CSIDL_COMMON_ALTSTARTUP= $001e; { non localized common startup }
  CSIDL_COMMON_FAVORITES  = $001f; { User favourites }
  CSIDL_INTERNET_CACHE       = $0020; { temporary inter files }
  CSIDL_COOKIES                    = $0021; { <user name>\Local Settings\Application Data\
                                                                    ..\cookies }
  CSIDL_HISTORY                    = $0022; { <user name>\Local Settings\
                                                                       Application Data\..\history}
  CSIDL_COMMON_APPDATA     = $0023; { All Users\Application Data }
  CSIDL_WINDOWS                  = $0024; { GetWindowsDirectory() }
  CSIDL_SYSTEM                      = $0025; { GetSystemDirectory() }
  CSIDL_PROGRAM_FILES         = $0026; { C:\Program Files }
  CSIDL_MYPICTURES               = $0027; { C:\Program Files\My Pictures }
  CSIDL_PROFILE                      = $0028; { USERPROFILE }
  CSIDL_SYSTEMX86                 = $0029; { x86 system directory on RISC }
  CSIDL_PROGRAM_FILESX86    = $002a; { x86 C:\Program Files on RISC }
  CSIDL_PROGRAM_FILES_COMMON   = $002b; { C:\Program Files\Common }
  CSIDL_PROGRAM_FILES_COMMONX86 = $002c; { x86 C:\Program Files\Common on RISC }
  CSIDL_COMMON_TEMPLATES              = $002d; { All Users\Templates }
  CSIDL_COMMON_DOCUMENTS            = $002e; { All Users\Documents }
  CSIDL_COMMON_ADMINTOOLS           = $002f; { All Users\Start Menu\Programs\
                                                                            Administrative Tools }
  CSIDL_ADMINTOOLS                          = $0030; { <user name>\Start Menu\Programs\
                                                                              Administrative Tools }
  CSIDL_CONNECTIONS                        = $0031; { Network and Dial-up Connections }
  CSIDL_COMMON_MUSIC                     = $0035; { All Users\My Music }
  CSIDL_COMMON_PICTURES                = $0036; { All Users\My Pictures }
  CSIDL_COMMON_VIDEO                     = $0037; { All Users\My Video }
  CSIDL_RESOURCES                            = $0038; { Resource Directory }
  CSIDL_RESOURCES_LOCALIZED          = $0039; { Localized Resource Directory }
  CSIDL_CDBURN_AREA                        = $003b; { USERPROFILE\Local Settings\
                                                                       Application Data\Microsoft\CD Burning }
  
By using GetEnvironmentVariable function
GetEnvironmentVariable function returns Environment Variables value stored in Windows System. We just need to pass Environment variable name like WINDIR, TEMP, USERPROFILE. We need to use Sysutils unit in uses clause.

function GetEnvironmentVariable(const Name: string): string;

For example we will call this function to get windows directory.
procedure TForm1.Button1Click(Sender: TObject);
begin
  sWindowsDir := GetEnvironmentVariable('WINDIR');
  ShowMessage(sWindowsDir);
end;

However we can use GetEnvironmentVariable function to get some extra information from system like Computer name, User Name etc. Following are environment variable lists we can use with GetEnvironmentVariable .

ALLUSERSPROFILE
APPDATA
CLIENTNAME
COMMONPROGRAMFILES
COMPUTERNAME
COMSPEC
HOMEDRIVE
HOMEPATH
LOGONSERVER
NUMBER_OF_PROCESSORS
OS
PATH
PATHEXT
PCTOOLSDIR
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
PROGRAMFILES
SESSIONNAME
SYSTEMDRIVE
SYSTEMROOT
TEMP
TMP
USERDOMAIN
USERNAME
USERPROFILE
WINDIR

By using other Window's API functions Delphi provides. We need to use Windows Unit in uses clause.

{ Getting the Windows Directory } 
function GetWinDir: string; 
var 
  dir: array [0..MAX_PATH] of Char; 
begin 
  GetWindowsDirectory(dir, MAX_PATH); 
  Result := StrPas(dir); 
end; 

{ Getting the System Directory } 
function SystemDir: string; 
var 
  dir: array [0..MAX_PATH] of Char; 
begin 
  GetSystemDirectory(dir, MAX_PATH); 
  Result := StrPas(dir); 
end; 

{ Getting the Temporary Directory } 
function GetTempDir: string; 
var 
  Buffer: array[0..MAX_PATH] of Char; 
begin 
  GetTempPath(SizeOf(Buffer) - 1, Buffer); 
  Result := StrPas(Buffer); 

end; 

Comments

  1. Boas dicas. Porém não tem como saber a pasta de "DownLoads"!? Quem sabe?
    GetEnvironement('USERPROFILE')+'\DownLoads' Será? Funciona se estiveres a usar o valor por defeito.
    Todavia já não caso tenha alterado o path the Downloads, por exemplo de C:\ para D:\ já não!
    Alguma ideia?

    ReplyDelete

Post a Comment

Popular posts from this blog

MS Excel Automation in Delphi

ShellExecute in Delphi

Drawing Shapes in Delphi