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

8

我怎样可以获取appdata文件夹路径?这是我的代码:

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

但是没有起作用。
3个回答

14

你无法直接将环境变量传递给 WinExec()。你需要先解析它们,例如:

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;

或者:

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()代替,例如:

但无论如何,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;

为什么不使用 "System.SysUtils.GetHomePath"? - Gabriel
在 Delphi 7 中不可用。 - monnoo

1

正确的方法是使用 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;

AppName是我的程序名吗?如果是,我会通过参数传递它。 - Linces Marques
1
“AppName是我的程序名称吗?” 是的。 - Gabriel

0

SHGetKnownFolderPath

SHGetKnownFolderPath

是一个用于检索特定已知文件夹路径的函数。它是在 Windows Vista 中引入的,并在后续版本中得到了支持。该函数可以帮助开发人员编写更加可移植和兼容的代码,因为它可以自动适应不同的操作系统版本和语言环境。如果您正在开发 Windows 应用程序,那么 SHGetKnownFolderPath 可能会对您有所帮助。
program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  Classes;

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接