How to use Windows variables in Delphi?

3
Hello, I'm creating a project in Delphi , however, it needs to create some files, it would not give a good impression if it did this in the place where it is, so I need these files to be created in the temporary Windows folder to avoid any problems. Ten of thanks for reading my question.

    
asked by anonymous 06.06.2015 / 19:27

1 answer

5

For information about the environment, use the function GetEnvironmentVariable , according to this page , you can use it from the following mode:

function GetEnvVarValue(const VarName: string): string;
var
  BufSize: Integer;  
begin
  BufSize := GetEnvironmentVariable(PChar(VarName), nil, 0);
  if BufSize > 0 then
  begin
    SetLength(Result, BufSize - 1);
    GetEnvironmentVariable(PChar(VarName),
      PChar(Result), BufSize);
  end
  else
    Result := '';
end;

Example usage:

ShowMessage(GetEnvVarValue('windir')); // Exibe a localização do diretório do Windows

The values you can pass as arguments to the above function are:

  • 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

A list of variables and their default values can be seen in this Wikipedia page .

    
06.06.2015 / 19:34