Delphi 柏林 10.1 创建 file/folder OS X

Delphi Berlin 10.1 create file/folder OS X

如何通过 delphi 在 OS X 上创建 file/folder? 我正在尝试使用函数

System.SysUtils.FileCreate

System.IOUtils.TDirectory.CreateDirectory

代码:

  if FileCreate('/var/folders/bla.txt') = INVALID_HANDLE_VALUE then
 raise Exception.Create('File already exists');

 `TDirectory.CreateDirectory('/var/folders/mydirdelete');`

无论如何,当我 运行 应用程序时,我得到

Exception EFileNotFoundException in module blprtl240.dylib at 0053AFAF.
The specified file was not found. 

即使我创建了文件夹。

有什么建议吗?

文档说 FileCreate return INVALID_HANDLE_VALUE (-1) 表示发生错误,这不仅意味着该文件已经存在,例如可能是 Permission denied ...等等

如果给定的路径无效或包含无效字符,CreateDirectory 将引发异常。

试试这个:

const
  AFilename = '/tmp/folders/bla.txt';
begin
  if NOT FileExists(AFilename) then begin
    if NOT DirectoryExists(ExtractFilePath(AFilename)) then
      if NOT ForceDirectories(ExtractFilePath(AFilename)) then RaiseLastOSError;
    if (FileCreate(AFilename) = INVALID_HANDLE_VALUE) then RaiseLastOSError;
  end;