加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi-7 – 如何在delphi中获取appdata文件夹路径

发布时间:2020-12-15 09:35:10 所属栏目:大数据 来源:网络整理
导读:我如何获得appdata文件夹路径?这个id我的代码: beginWinexec(PAnsichar('%appdata%TEST.exe'),sw_show);end;end. 但没有工作. 解决方法 您无法将环境变量传递给WinExec().你必须先解决它们: uses ...,SysUtils;function GetPathToTestExe: string;begin
我如何获得appdata文件夹路径?这个id我的代码:

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

但没有工作.

解决方法

您无法将环境变量传递给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
  ...,Windows,SHFolder;

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

但是,无论如何,自Windows 95以来,WinExec()已被弃用,你真的应该使用CreateProcess()代替:

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,FALSE,@si,pi)
  begin
    //...
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
  end;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读