How to get appdata folder path in delphi

How can I get the path to the appdata folder? this id is my code:

begin
Winexec(PAnsichar('%appdata%\TEST.exe'), sw_show);
end;
end.

but does not work.

+4
source share
2 answers

You cannot pass environment variables to WinExec(). First you have to solve them:

uses
  ..., SysUtils;

function GetPathToTestExe: string;
begin
  Result := SysUtils.GetEnvironmentVariable('APPDATA');
  if Result <> '' then
    Result := IncludeTrailingPathDelimiter(Result) + 'TEST.exe';
end;

uses
  ..., Windows;

var
  Path: string;
begin
  Path = GetPathToTestExe;
  if Path <> '' then
    WinExec(PAnsiChar(Path), SW_SHOW);
end;

As an alternative:

uses
  ..., SysUtils, Windows;

function GetPathToTestExe: string;
var
  Path: array[0..MAX_PATH+1] of Char;
begin
  if ExpandEnvironmentStrings('%APPDATA%', Path, Length(Path)) > 1 then
    Result := IncludeTrailingPathDelimiter(Path) + 'TEST.exe'
  else
    Result := '';
end;

A more reliable (and official) way to get the path to the APPDATA folder is to use SHGetFolderPath()(or SHGetKnownFolderPath()in Vista +) instead of:

uses
  ..., SysUtils, Windows, SHFolder;

function GetPathToTestExe: string;
var
  Path: array[0..MAX_PATH] of Char;
begin
  if SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, Path) = S_OK then
    Result := IncludeTrailingPathDelimiter(Path) + 'TEST.exe'
  else
    Result := '';
end;

But either way is WinExec()deprecated with Windows 95, you really should use CreateProcess()instead:

uses
  ..., Windows;

var
  Path: String;
  si: TStartupInfo;
  pi: TProcessInformation;

Path := GetPathToTetExe;
if Path <> '' then
begin
  ZeroMemory(@si, SizeOf(si));
  si.cb := SizeOf(si);
  si.dwFlags := STARTF_USESHOWWINDOW;
  si.wShowWindow := SW_SHOW;

  if CreateProcess(nil, PChar(Path), nil, nil, FALSE, 0, nil, nil, @si, pi)
  begin
    //...
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
  end;
end;
+8
source

The correct (multi-platform) way to do this is using System.IOUtils:

function GetAppDataFolder: string;                                       { Returns the path to the current user AppData folder on Windows and to the current user home directory on Mac OS X.   Example:  c:\Documents and Settings\Bere\Application Data\AppName\ }
begin
 Assert(System.IOUtils.TPath.HasValidFileNameChars(AppName, FALSE), 'Invalid chars in AppName: '+ AppName);
 Result:= Trail(Trail(System.SysUtils.GetHomePath)+ AppName);
end;

Utils:

function ForceAppDataFolder: string;  // Make sure the AppDataFolder exists before you try to write the INI file there!                                      
begin
 Result:= GetAppDataFolder;
 ForceDirectories(Result);
end;

function Trail(CONST Path: string): string;    //ok  Works with UNC paths
begin
 if Path= '' then EXIT('');                                                                        { Encountered when doing something like this:  ExtractLastFolder('c:\'). ExtractLastFolder will return '' }
 Result:= IncludeTrailingPathDelimiter(Path)
end;
0
source

All Articles