Expand Env Variables In Path - Delphi

DarkCoderSc
Jean-Pierre LESUEUR
uses
uHelper, System.SysUtils, Winapi.Windows;
// ...
function ExpandPath(const APath : String) : String;
begin
var APathLength := ExpandEnvironmentStrings(PWideChar(APath), nil, 0);
if APathLength = 0 then
Exit(APath);
///
SetLength(Result, APathLength - 1);
if ExpandEnvironmentStrings(PWideChar(APath), PWideChar(result), APathLength) = 0 then
result := APath;
///
result := IncludeTrailingPathDelimiter(result);
end;
// ...
begin
try
WriteLn(ExpandPath('%TEMP%'));
WriteLn(ExpandPath('C:\Users\%USERNAME%\'));
except
on e : Exception do
WriteLn(e.Message);
end;
// ...
Creating and researching code snippets takes time and effort. You’re welcome to share them through your own platforms, but please don’t forget to credit the original author, here: Jean-Pierre LESUEUR.
Implemented By Technique
Featured Windows API
Created
September 17, 2025
Last Revised
September 17, 2025