How to use Windows variables in Delphi?

Asked

Viewed 3,054 times

4

Hello, I’m creating a project on Delphi, However, it needs to create some files, it wouldn’t give a good impression if it did this where it is, so I need these files to be created in the temporary Windows folder to avoid any issues. Thank you for reading my question.

  • Variables of windows environment?

1 answer

5


For information about the environment, use the function GetEnvironmentVariable, according to that page, you can use it as follows:

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 of use:

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

The values you can pass as argument 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.

  • Thanks friend!

  • I’ll do it once the site releases, and again thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.