IOS下面如果文件夹中的文件名包含特殊字符Delphi无法取到
Submitted by hubdog on Sat, 2020-03-21 17:22
TDirectory.GetFiles方法用的是linux的函数,对特殊字符的文件会认为是目录导致无法取得列表
下面这个函数用NSFileManager绕过了这个问题
function GetFiles(APath:string):TStringDynArray; var fm:NSFileManager; files:NSArray; I: Integer; list:TStrings; f:string; isDir:boolean; begin fm := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager); files:=fm.contentsOfDirectoryAtPath(StrToNSStr(APath), nil); list:=TStringList.Create; try for I := 0 to files.count-1 do begin f:=TPath.Combine(APath, NSStrToStr(TNSString.Wrap(files.objectAtIndex(i)))); isDir:=False; fm.fileExistsAtPath(StrToNSStr(f), @isDir); if isDir then Continue; list.Add(f); end; SetLength(result, list.count); for I := 0 to list.count-1 do begin result[i]:=list[I]; end; finally list.Free; end; end; function GetDirectories(APath:string):TStringDynArray; var fm:NSFileManager; files:NSArray; I: Integer; list:TStrings; f:string; isDir:boolean; begin fm := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager); files:=fm.contentsOfDirectoryAtPath(StrToNSStr(APath), nil); list:=TStringList.Create; try for I := 0 to files.count-1 do begin f:=TPath.Combine(APath, NSStrToStr(TNSString.Wrap(files.objectAtIndex(i)))); isDir:=False; fm.fileExistsAtPath(StrToNSStr(f), @isDir); if not isDir then Continue; list.Add(f); end; SetLength(result, list.count); for I := 0 to list.count-1 do begin result[i]:=list[I]; end; finally list.Free; end; end;