来源:天空软件网 更新:2023-09-14
用手机看
Lazarus 的 FileUtil 单元包含用以维持 Delphi 的 FileUtil 单元的兼容性的函数和过程。
为在一个工程中启动 FileUtil,只需要将 LazUtils 添加到所需要的软件包中。按照这个步骤:
1- 转到Lazarus IDE 菜单" Project > Project Inspector "。
2- 在"工程查看器"对话框窗口中,单击" Add > New Requirement "。
3- 在" New Requirement "对话框窗口中,查找LazUtils单元,然后单击OK。
4- 现在,你可以将 FileUtil 添加到一个 Uses 分句之中。
// 文件属性或状态 function CompareFilenames(const Filename1, Filename2: string): integer; inline; function CompareFilenamesIgnoreCase(const Filename1, Filename2: string): integer; inline; function CompareFilenames(const Filename1, Filename2: string; ResolveLinks: boolean): integer; function CompareFilenames(Filename1: PChar; Len1: integer; Filename2: PChar; Len2: integer; ResolveLinks: boolean): integer; function FilenameIsAbsolute(const TheFilename: string):boolean; inline; function FilenameIsWinAbsolute(const TheFilename: string):boolean; inline; function FilenameIsUnixAbsolute(const TheFilename: string):boolean; inline; procedure CheckIfFileIsExecutable(const AFilename: string); inline; procedure CheckIfFileIsSymlink(const AFilename: string); inline; function FileIsReadable(const AFilename: string): boolean; inline; function FileIsWritable(const AFilename: string): boolean; inline; function FileIsText(const AFilename: string): boolean; inline; function FileIsText(const AFilename: string; out FileReadable: boolean): boolean; inline; function FileIsExecutable(const AFilename: string): boolean; inline; function FileIsSymlink(const AFilename: string): boolean; inline; function FileIsHardLink(const AFilename: string): boolean; inline; function FileSize(const Filename: string): int64; overload; inline; function GetFileDescription(const AFilename: string): string; inline; function ReadAllLinks(const Filename: string; ExceptionOnError: boolean): string; //如果一个链接是破损的,返回'' function TryReadAllLinks(const Filename: string): string; //如果一个链接是破损的,返回文件名称 // 目录 function DirPathExists(const FileName: String): Boolean; inline; function ForceDirectory(DirectoryName: string): boolean; inline; function DeleteDirectory(const DirectoryName: string; OnlyChildren: boolean): boolean; function ProgramDirectory: string; function DirectoryIsWritable(const DirectoryName: string): boolean; inline; // 文件名称部分 function ExtractFileNameOnly(const AFilename: string): string; inline; function ExtractFileNameWithoutExt(const AFilename: string): string; function CompareFileExt(const Filename, Ext: string; CaseSensitive: boolean): integer; overload; inline; function CompareFileExt(const Filename, Ext: string): integer; overload; inline; function FilenameIsPascalUnit(const Filename: string): boolean; function AppendPathDelim(const Path: string): string; inline; function ChompPathDelim(const Path: string): string; inline; function TrimFilename(const AFilename: string): string; inline; function CleanAndExpandFilename(const Filename: string): string; inline; function CleanAndExpandDirectory(const Filename: string): string; inline; function CreateAbsoluteSearchPath(const SearchPath, BaseDirectory: string): string; function CreateRelativePath(const Filename, BaseDirectory: string; UsePointDirectory: boolean = false; AlwaysRequireSharedBaseFolder: Boolean = True): string; inline; function CreateAbsolutePath(const Filename, BaseDirectory: string): string; function FileIsInPath(const Filename, Path: string): boolean; function FileIsInDirectory(const Filename, Directory: string): boolean; // 文件搜索 type TSearchFileInPathFlag = ( sffDontSearchInBasePath, sffSearchLoUpCase ); TSearchFileInPathFlags = set of TSearchFileInPathFlag; const AllDirectoryEntriesMask = '*'; function GetAllFilesMask: string; inline; function GetExeExt: string; inline; function SearchFileInPath(const Filename, BasePath, SearchPath, Delimiter: string; Flags: TSearchFileInPathFlags): string; function SearchAllFilesInPath(const Filename, BasePath, SearchPath, Delimiter: string; Flags: TSearchFileInPathFlags): TStrings; function FindDiskFilename(const Filename: string): string; function FindDiskFileCaseInsensitive(const Filename: string): string; function FindDefaultExecutablePath(const Executable: string; const BaseDir: string = ''): string; type TFileIterator = class private FPath: String; FLevel: Integer; FFileInfo: TSearchRec; FSearching: Boolean; function GetFileName: String; public procedure Stop; function IsDirectory: Boolean; public property FileName: String read GetFileName; property FileInfo: TSearchRec read FFileInfo; property Level: Integer read FLevel; property Path: String read FPath; property Searching: Boolean read FSearching; end; TFileFoundEvent = procedure (FileIterator: TFileIterator) of object; TDirectoryFoundEvent = procedure (FileIterator: TFileIterator) of object; TDirectoryEnterEvent = procedure (FileIterator: TFileIterator) of object; TFileSearcher = class(TFileIterator) private FMaskSeparator: char; FFollowSymLink: Boolean; FOnFileFound: TFileFoundEvent; FOnDirectoryFound: TDirectoryFoundEvent; FOnDirectoryEnter: TDirectoryEnterEvent; FFileAttribute: Word; FDirectoryAttribute: Word; procedure RaiseSearchingError; protected procedure DoDirectoryEnter; virtual; procedure DoDirectoryFound; virtual; procedure DoFileFound; virtual; public constructor Create; procedure Search(const ASearchPath: String; ASearchMask: String = ''; ASearchSubDirs: Boolean = True; CaseSensitive: Boolean = False); public property MaskSeparator: char read FMaskSeparator write FMaskSeparator; property FollowSymLink: Boolean read FFollowSymLink write FFollowSymLink; property FileAttribute: Word read FFileAttribute write FFileAttribute default faAnyfile; property DirectoryAttribute: Word read FDirectoryAttribute write FDirectoryAttribute default faDirectory; property OnDirectoryFound: TDirectoryFoundEvent read FOnDirectoryFound write FOnDirectoryFound; property OnFileFound: TFileFoundEvent read FOnFileFound write FOnFileFound; property OnDirectoryEnter: TDirectoryEnterEvent read FOnDirectoryEnter write FOnDirectoryEnter; end; function <>( const SearchPath: String; SearchMask: String = ''; SearchSubDirs: Boolean = True): TStringList; function FindAllDirectories(const SearchPath: string; SearchSubDirs: Boolean = True): TStringList; // 复制一个文件或一整个目录树 function <>(const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=): boolean; function <>(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean; function CopyDirTree(const SourceDir, TargetDir: string; Flags: TCopyFileFlags=<>): Boolean; // 文件动作 function ReadFileToString(const Filename: string): string; function GetTempFilename(const Directory, Prefix: string): string; inline; //基本的函数,类似于 RTL,但是使用 UTF-8,而不是使用系统编码 //AnsiToUTF8和UTF8ToAnsi在Linux、BSD、MacOSX 下需要一个 widestring 管理器 //但是通常情况下,这些操作系统使用 UTF-8 作为系统编码,因此不需要 widestring 管理器。 function NeedRTLAnsi: boolean; inline;// true,如果系统编码不是UTF-8 procedure SetNeedRTLAnsi(NewValue: boolean); inline; function UTF8ToSys(const s: string): string; inline;//像UTF8ToAnsi一样,但是更多的独立于widestring管理器 function SysToUTF8(const s: string): string; inline;//像AnsiToUTF8一样,但是更多的独立于widestring管理器 function ConsoleToUTF8(const s: string): string; inline;//转换OEM编码字符串到UTF8 (与一些Windows的特殊函数一起使用) function UTF8ToConsole(const s: string): string; inline;//转换UTF8字符串到控制台编码(由Write、WriteLn使用) // 文件操作 function FileExistsUTF8(const Filename: string): boolean; inline; function FileAgeUTF8(const FileName: string): Longint; inline; function DirectoryExistsUTF8(const Directory: string): Boolean; inline; function ExpandFileNameUTF8(const FileName: string): string; inline; function ExpandUNCFileNameUTF8(const FileName: string): string; function ExtractShortPathNameUTF8(Const FileName : String) : String; function FindFirstUTF8(const Path: string; Attr: Longint; out Rslt: TSearchRec): Longint; inline; function FindNextUTF8(var Rslt: TSearchRec): Longint; inline; procedure FindCloseUTF8(var F: TSearchrec); inline; function FileSetDateUTF8(const FileName: String; Age: Longint): Longint; inline; function FileGetAttrUTF8(const FileName: String): Longint; inline; function FileSetAttrUTF8(const Filename: String; Attr: longint): Longint; inline; function DeleteFileUTF8(const FileName: String): Boolean; inline; function RenameFileUTF8(const OldName, NewName: String): Boolean; inline; function FileSearchUTF8(const Name, DirList : String; ImplicitCurrentDir : Boolean = True): String; inline; function FileIsReadOnlyUTF8(const FileName: String): Boolean; inline; function GetCurrentDirUTF8: String; inline; function SetCurrentDirUTF8(const NewDir: String): Boolean; inline; function CreateDirUTF8(const NewDir: String): Boolean; inline; function RemoveDirUTF8(const Dir: String): Boolean; inline; function ForceDirectoriesUTF8(const Dir: string): Boolean; inline; function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle; inline; function FileCreateUTF8(Const FileName : string) : THandle; overload; inline; function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle; overload; inline; // 环境 function ParamStrUTF8(Param: Integer): string; inline; function GetEnvironmentStringUTF8(Index: Integer): string; inline; function GetEnvironmentVariableUTF8(const EnvVar: string): String; inline; function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string; inline; function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false; CreateDir: boolean = false): string; inline; // 其他 function SysErrorMessageUTF8(ErrorCode: Integer): String; inline;
专门处理 UTF8 文件的文件例行程序应使用 LazFileUtils 单元。
function CompareFilenames(const Filename1, Filename2: string): integer; overload;function CompareFilenamesIgnoreCase(const Filename1, Filename2: string): integer;function CompareFileExt(const Filename, Ext: string; CaseSensitive: boolean): integer; overload;function CompareFileExt(const Filename, Ext: string): integer; overload;function CompareFilenameStarts(const Filename1, Filename2: string): integer;function CompareFilenames(Filename1: PChar; Len1: integer; Filename2: PChar; Len2: integer): integer; overload;function CompareFilenamesP(Filename1, Filename2: PChar; IgnoreCase: boolean = false): integer; // false = 使用默认
function DirPathExists(DirectoryName: string): boolean;function DirectoryIsWritable(const DirectoryName: string): boolean;
function ExtractFileNameOnly(const AFilename: string): string;function FilenameIsAbsolute(const TheFilename: string):boolean;function FilenameIsWinAbsolute(const TheFilename: string):boolean;function FilenameIsUnixAbsolute(const TheFilename: string):boolean;function ForceDirectory(DirectoryName: string): boolean;procedure CheckIfFileIsExecutable(const AFilename: string);procedure CheckIfFileIsSymlink(const AFilename: string);function FileIsExecutable(const AFilename: string): boolean;function FileIsSymlink(const AFilename: string): boolean;function FileIsHardLink(const AFilename: string): boolean;function FileIsReadable(const AFilename: string): boolean;function FileIsWritable(const AFilename: string): boolean;function FileIsText(const AFilename: string): boolean;function FileIsText(const AFilename: string; out FileReadable: boolean): boolean;function FilenameIsTrimmed(const TheFilename: string): boolean;function FilenameIsTrimmed(StartPos: PChar; NameLen: integer): boolean;function TrimFilename(const AFilename: string): string;function ResolveDots(const AFilename: string): string;Procedure ForcePathDelims(Var FileName: string);Function GetForcedPathDelims(Const FileName: string): String;function CleanAndExpandFilename(const Filename: string): string; //空的字符串返回当前目录function CleanAndExpandDirectory(const Filename: string): string; //空的字符串返回当前目录function TrimAndExpandFilename(const Filename: string; const BaseDir: string = ''): string; //空的字符串返回空的字符串function TrimAndExpandDirectory(const Filename: string; const BaseDir: string = ''): string; //空的字符串返回空的字符串function TryCreateRelativePath(const Dest, Source: String; UsePointDirectory: boolean; AlwaysRequireSharedBaseFolder: Boolean; out RelPath: String): Boolean;function CreateRelativePath(const Filename, BaseDirectory: string;UsePointDirectory: boolean = false; AlwaysRequireSharedBaseFolder: Boolean = True): string;function FileIsInPath(const Filename, Path: string): boolean;function AppendPathDelim(const Path: string): string;function ChompPathDelim(const Path: string): string;
function CreateAbsoluteSearchPath(const SearchPath, BaseDirectory: string): string;function CreateRelativeSearchPath(const SearchPath, BaseDirectory: string): string;function MinimizeSearchPath(const SearchPath: string): string;function FindPathInSearchPath(APath: PChar; APathLen: integer; SearchPath: PChar; SearchPathLen: integer): PChar; overload;function FindPathInSearchPath(const APath, SearchPath: string): integer; overload;
function FileExistsUTF8(const Filename: string): boolean;function FileAgeUTF8(const FileName: string): Longint;function DirectoryExistsUTF8(const Directory: string): Boolean;function ExpandFileNameUTF8(const FileName: string; {const} BaseDir: string = ''): string;function FindFirstUTF8(const Path: string; Attr: Longint; out Rslt: TSearchRec): Longint;function FindNextUTF8(var Rslt: TSearchRec): Longint;procedure FindCloseUTF8(var F: TSearchrec); inline;
function FileSetDateUTF8(const FileName: String; Age: Longint): Longint;function FileGetAttrUTF8(const FileName: String): Longint;function FileSetAttrUTF8(const Filename: String; Attr: longint): Longint;function DeleteFileUTF8(const FileName: String): Boolean;function RenameFileUTF8(const OldName, NewName: String): Boolean;function FileSearchUTF8(const Name, DirList : String; ImplicitCurrentDir : Boolean = True): String;function FileIsReadOnlyUTF8(const FileName: String): Boolean;
function GetCurrentDirUTF8: String;function SetCurrentDirUTF8(const NewDir: String): Boolean;function CreateDirUTF8(const NewDir: String): Boolean;function RemoveDirUTF8(const Dir: String): Boolean;function ForceDirectoriesUTF8(const Dir: string): Boolean;
function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle;function FileCreateUTF8(Const FileName : string) : THandle; overload;function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle; overload;Function FileCreateUtf8(Const FileName : String; ShareMode : Integer; Rights : Cardinal) : THandle; overload;
function FileSizeUtf8(const Filename: string): int64;function GetFileDescription(const AFilename: string): string;function ReadAllLinks(const Filename: string; {%H-}ExceptionOnError: boolean): string; //如果一个链接是破损的,返回''function TryReadAllLinks(const Filename: string): string; //如果一个链接是破损的,返回文件名称function GetShellLinkTarget(const FileName: string): string;
function DbgSFileAttr(Attr: LongInt): String;TPhysicalFilenameOnError = (pfeException,pfeEmpty,pfeOriginal); //获取FileNameOnError
// Getting file name except for Unixfunction GetPhysicalFilename(const Filename: string; OnError: TPhysicalFilenameOnError): string;// for Unixfunction GetUnixPhysicalFilename(const Filename: string; ExceptionOnError: boolean): string; //如果一个链接是破损的,返回''
function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string;function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false; CreateDir: boolean = false): string;function GetTempFileNameUTF8(const Dir, Prefix: String): String;// UNC路径function IsUNCPath(const {%H-}Path: String): Boolean;function ExtractUNCVolume(const {%H-}Path: String): String;function ExtractFileRoot(FileName: String): String;// Darwin (macOS)路径function GetDarwinSystemFilename(Filename: string): string;function GetDarwinNormalizedFilename(Filename: string; nForm:Integer=2): string;// Windows路径function SHGetFolderPathUTF8(ID : Integer) : String;
procedure SplitCmdLineParams(const Params: string; ParamList: TStrings; ReadBackslash: boolean = false);function StrToCmdLineParam(const Param: string): string;function MergeCmdLineParams(ParamList: TStrings): string;
TInvalidateFileStateCacheEvent = procedure(const Filename: string); var OnInvalidateFileStateCache: TInvalidateFileStateCacheEvent = nil;procedure InvalidateFileStateCache(const Filename: string = ''); inline;