For Newbie

Tuesday, 15 November 2011

Accessing Environment Variables Windows Delphi



Accessing Environment Variables Windows% AppData%,% UserProfile%,% AllUsersProfile%, etc..

Environment Variables are variables path handler of Windows, such as% AppData%,% UserProfile%,% AllUsersProfile%,% SystemRoot%,% programfiles% and so on. These variables store the value / value of working directories of Windows, eg: Program Files, System32, etc. Application Data.

Function Declaration:
const
   sEnv: array [1 .. 8] of string = ('% AppData %','% %','% AllUsersProfile% UserProfile', '% ProgramFiles%', '% SystemDrive% SystemRoot %','%', '% Temp% %','% tmp ');

EnvVar function (const v: string): string;
var
   c: array [0 .. 1023] of Char;
begin
   if ExpandEnvironmentStrings (PChar (v), c, 1024) = 0 then
   Result: = v
   else Result: = Trim (c);
end;

Example of implementation:
TPX_FA.FormCreate procedure (Sender: TObject);
var
   i: Byte;
begin
   for i: = 1 to 8 do
   begin
     Memo1.Lines.Add (sEnv [i] + ':' + EnvVar (sEnv [i]));
   end;
end;


output:



Accessing Environment Variables Windows

Share on Facebook
Share on Twitter
Share on Google+

Related : Accessing Environment Variables Windows Delphi