windows 如何在 Delphi 中获取appdata文件夹路径

jogvjijk  于 2022-11-26  发布在  Windows
关注(0)|答案(3)|浏览(296)

如何获取appdata文件夹路径?这是我的代码:

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

但不工作。

pnwntuvh

pnwntuvh1#

您不能将环境变量传递给WinExec()。您必须先解析它们,例如:
第一个
或者:

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;

获取APPDATA文件夹路径的更可靠(也是正式的)方法是使用SHGetFolderPath()(或Vista+上的SHGetKnownFolderPath()),例如:

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;

或者:

uses
  ..., SysUtils;

function GetPathToTestExe: string;
var
  Path: string;
begin
  // GetHomePath() uses SHGetFolderPath(CSIDL_APPDATA) internally...
  Path := SysUtils.GetHomePath;
  if Path <> '' then
    Result := IncludeTrailingPathDelimiter(Path) + 'TEST.exe'
  else
    Result := '';
end;

但是,无论如何,WinExec()自Windows 95以来已被弃用,您实际上应该使用CreateProcess(),例如:

uses
  ..., Windows;

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

Path := GetPathToTestExe;
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;
6za6bjd0

6za6bjd02#

正确的方法是使用System.IOUtils:

function GetAppDataFolder: string;                                       { Returns the path to the current user's AppData folder on Windows and to the current user's 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;

公用程式:

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;
643ylb08

643ylb083#

SHGetKnownFolderPath

program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  Classes
  { you can add units after this };

function SHGetKnownFolderPath(const rfid: TGuid; dwFlags: DWORD; hToken: THandle; out ppszPath: PWideChar): HRESULT; stdcall; external 'shell32.dll' name 'SHGetKnownFolderPath';

const
  localAppdataGuid: TGuid = '{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}';
var
  ppszPath: PWideChar;
begin
  SHGetKnownFolderPath(localAppdataGuid, 0, 0, ppszPath);
  Writeln(string(ppszPath));
  Readln;
end.

对于另一个文件夹guid KNOWNFOLDERID

相关问题