gpt4 book ai didi

macos - 在Delphi中获取MacOS Application Support文件夹

转载 作者:行者123 更新时间:2023-12-02 15:56:34 25 4
gpt4 key购买 nike

Apple 表示将存储应用程序状态的文件放置在“Application Support”文件夹中,并“使用应用程序支持目录常量 NSApplicationSupportDirectory”

https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW11

Delphi 的 TPath 类方法有各种 TPath.GetXXXX(包括 TPath.GetLibraryPath,但我找不到返回应用程序支持文件夹的方法。

如何在 Firemonkey 应用程序中获取 Application Support 文件夹?

最佳答案

硬编码方法(您可能已经尝试过);因为 Application Support 文件夹位于 Library 文件夹下:

uses System.IOUtils;

function GetApplicationSupportDir : string;

begin
Result := TPath.Combine(TPath.GetLibraryPath,'Application Support');
end;

或者,直接从 iOS 或 OSX 检索:(对于 Delphi XE8 及更高版本)

uses
Macapi.Helpers,
{$IFDEF iOS}
iOSapi.Foundation,
{$ENDIF}
{$IFDEF OSX}
Macapi.Foundation,
{$ENDIF}

System.IOUtils;

function GetApplicationSupportDir : string;

var
Paths : NSArray;
Dir : NSString;

begin
// For "Application Support" under the User's Library directory:
Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True));

// For "Application Support" under the System Library directory:
//Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSSystemDomainMask, True));


Dir := TNSString.Wrap(Paths.objectAtIndex(0));

Result := NSStrToStr(Dir);
end;
<小时/>

对于 XE7,这可能有效:

uses
Macapi.Helpers,
{$IFDEF iOS}
iOSapi.Foundation,
{$ENDIF}
{$IFDEF OSX}
Macapi.Foundation,
{$ENDIF}

System.IOUtils;

///////////////////////////// Added since XE7:

const
_PU = '_';
libFoundation = '/System/Library/Frameworks/Foundation.framework/Foundation';

type
NSUInteger = LongWord;
NSSearchPathDirectory = NSUInteger;
NSSearchPathDomainMask = NSUInteger;

function NSSearchPathForDirectoriesInDomains(directory: NSSearchPathDirectory; domainMask: NSSearchPathDomainMask;
expandTilde: Boolean): Pointer {NSArray}; cdecl;
external libFoundation name _PU + 'NSSearchPathForDirectoriesInDomains';

/////////////////////////////

function GetApplicationSupportDir : string;

var
Paths : NSArray;
Dir : NSString;

begin
// For "Application Support" under the User's Library directory:
Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True));

// For "Application Support" under the System Library directory:
//Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSSystemDomainMask, True));

Dir := TNSString.Wrap(Paths.objectAtIndex(0));

Result := NSStrToStr(Dir);
end;

关于macos - 在Delphi中获取MacOS Application Support文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45618686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com