简体   繁体   English

Delphi Berlin 10.1创建文件/文件夹OS X

[英]Delphi Berlin 10.1 create file/folder OS X

How to create file/folder on OS X via delphi? 如何通过delphi在OS X上创建文件/文件夹? I'm trying to use functions 我正在尝试使用功能

System.SysUtils.FileCreate

And

System.IOUtils.TDirectory.CreateDirectory

Code: 码:

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

or 要么

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

Anyway when i run app i get 无论如何,当我运行应用程序时,我得到了

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

Even if i create folder. 即使我创建文件夹。

Any advices? 有什么建议吗?

The docs says FileCreate return INVALID_HANDLE_VALUE ( -1 ) indicates that an error occurred which not only mean that file already exists eg it could be Permission denied ...etc. 文档说FileCreate返回INVALID_HANDLE_VALUE-1 )表示发生了一个错误,这不仅意味着该文件已经存在,例如,它可能被拒绝权限 ...等。

Regarding CreateDirectory raises an exception if the given Path is invalid or contains invalid characters. 如果给定的路径无效或包含无效字符,则关于CreateDirectory会引发异常。

Try this instead: 尝试以下方法:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM