fileutil/zh CN: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: fileutil/zh CN
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Lazarus的**FileUtil** 单元包含用以维持Delphi的FileUtil单元的兼容性的函数和过程。专门处理UTF8文件的文件例行程序应该转到[[LazFileUtils]]单元。 | |
| 2 | + | ||
| 3 | + | ## 函数列表 | |
| 4 | + | ``` | |
| 5 | + | //文件属性或状态 | |
| 6 | + | function CompareFilenames(const Filename1, Filename2: string): integer; inline; | |
| 7 | + | function CompareFilenamesIgnoreCase(const Filename1, Filename2: string): integer; inline; | |
| 8 | + | function CompareFilenames(const Filename1, Filename2: string; ResolveLinks: boolean): integer; | |
| 9 | + | function CompareFilenames(Filename1: PChar; Len1: integer; Filename2: PChar; Len2: integer; ResolveLinks: boolean): integer; | |
| 10 | + | function FilenameIsAbsolute(const TheFilename: string):boolean; inline; | |
| 11 | + | function FilenameIsWinAbsolute(const TheFilename: string):boolean; inline; | |
| 12 | + | function FilenameIsUnixAbsolute(const TheFilename: string):boolean; inline; | |
| 13 | + | procedure CheckIfFileIsExecutable(const AFilename: string); inline; | |
| 14 | + | procedure CheckIfFileIsSymlink(const AFilename: string); inline; | |
| 15 | + | function FileIsReadable(const AFilename: string): boolean; inline; | |
| 16 | + | function FileIsWritable(const AFilename: string): boolean; inline; | |
| 17 | + | function FileIsText(const AFilename: string): boolean; inline; | |
| 18 | + | function FileIsText(const AFilename: string; out FileReadable: boolean): boolean; inline; | |
| 19 | + | function FileIsExecutable(const AFilename: string): boolean; inline; | |
| 20 | + | function FileIsSymlink(const AFilename: string): boolean; inline; | |
| 21 | + | function FileIsHardLink(const AFilename: string): boolean; inline; | |
| 22 | + | function FileSize(const Filename: string): int64; overload; inline; | |
| 23 | + | function GetFileDescription(const AFilename: string): string; inline; | |
| 24 | + | function ReadAllLinks(const Filename: string; ExceptionOnError: boolean): string; //如果一个链接是破损的,返回'' | |
| 25 | + | function TryReadAllLinks(const Filename: string): string; //如果一个链接是破损的,返回文件名称 | |
| 26 | + | ||
| 27 | + | //目录 | |
| 28 | + | function DirPathExists(const FileName: String): Boolean; inline; | |
| 29 | + | function ForceDirectory(DirectoryName: string): boolean; inline; | |
| 30 | + | function DeleteDirectory(const DirectoryName: string; OnlyChildren: boolean): boolean; | |
| 31 | + | function ProgramDirectory: string; | |
| 32 | + | function DirectoryIsWritable(const DirectoryName: string): boolean; inline; | |
| 33 | + | ||
| 34 | + | //文件名称部分 | |
| 35 | + | function ExtractFileNameOnly(const AFilename: string): string; inline; | |
| 36 | + | function ExtractFileNameWithoutExt(const AFilename: string): string; | |
| 37 | + | function CompareFileExt(const Filename, Ext: string; CaseSensitive: boolean): integer; overload; inline; | |
| 38 | + | function CompareFileExt(const Filename, Ext: string): integer; overload; inline; | |
| 39 | + | function FilenameIsPascalUnit(const Filename: string): boolean; | |
| 40 | + | function AppendPathDelim(const Path: string): string; inline; | |
| 41 | + | function ChompPathDelim(const Path: string): string; inline; | |
| 42 | + | function TrimFilename(const AFilename: string): string; inline; | |
| 43 | + | function CleanAndExpandFilename(const Filename: string): string; inline; | |
| 44 | + | function CleanAndExpandDirectory(const Filename: string): string; inline; | |
| 45 | + | function CreateAbsoluteSearchPath(const SearchPath, BaseDirectory: string): string; | |
| 46 | + | function CreateRelativePath(const Filename, BaseDirectory: string; UsePointDirectory: boolean = false; AlwaysRequireSharedBaseFolder: Boolean = True): string; inline; | |
| 47 | + | function CreateAbsolutePath(const Filename, BaseDirectory: string): string; | |
| 48 | + | function FileIsInPath(const Filename, Path: string): boolean; | |
| 49 | + | function FileIsInDirectory(const Filename, Directory: string): boolean; | |
| 50 | + | ||
| 51 | + | //文件搜索 | |
| 52 | + | type | |
| 53 | + | TSearchFileInPathFlag = ( | |
| 54 | + | sffDontSearchInBasePath, | |
| 55 | + | sffSearchLoUpCase | |
| 56 | + | ); | |
| 57 | + | TSearchFileInPathFlags = set of TSearchFileInPathFlag; | |
| 58 | + | const | |
| 59 | + | AllDirectoryEntriesMask = '*'; | |
| 60 | + | ||
| 61 | + | function GetAllFilesMask: string; inline; | |
| 62 | + | function GetExeExt: string; inline; | |
| 63 | + | function SearchFileInPath(const Filename, BasePath, SearchPath, Delimiter: string; Flags: TSearchFileInPathFlags): string; | |
| 64 | + | function SearchAllFilesInPath(const Filename, BasePath, SearchPath, Delimiter: string; Flags: TSearchFileInPathFlags): TStrings; | |
| 65 | + | function FindDiskFilename(const Filename: string): string; | |
| 66 | + | function FindDiskFileCaseInsensitive(const Filename: string): string; | |
| 67 | + | function FindDefaultExecutablePath(const Executable: string; const BaseDir: string = ''): string; | |
| 68 | + | ||
| 69 | + | type | |
| 70 | + | TFileIterator = class | |
| 71 | + | private | |
| 72 | + | FPath: String; | |
| 73 | + | FLevel: Integer; | |
| 74 | + | FFileInfo: TSearchRec; | |
| 75 | + | FSearching: Boolean; | |
| 76 | + | function GetFileName: String; | |
| 77 | + | public | |
| 78 | + | procedure Stop; | |
| 79 | + | function IsDirectory: Boolean; | |
| 80 | + | public | |
| 81 | + | property FileName: String read GetFileName; | |
| 82 | + | property FileInfo: TSearchRec read FFileInfo; | |
| 83 | + | property Level: Integer read FLevel; | |
| 84 | + | property Path: String read FPath; | |
| 85 | + | property Searching: Boolean read FSearching; | |
| 86 | + | end; | |
| 87 | + | ||
| 88 | + | TFileFoundEvent = procedure (FileIterator: TFileIterator) of object; | |
| 89 | + | TDirectoryFoundEvent = procedure (FileIterator: TFileIterator) of object; | |
| 90 | + | TDirectoryEnterEvent = procedure (FileIterator: TFileIterator) of object; | |
| 91 | + | ||
| 92 | + | TFileSearcher = class(TFileIterator) | |
| 93 | + | private | |
| 94 | + | FMaskSeparator: char; | |
| 95 | + | FFollowSymLink: Boolean; | |
| 96 | + | FOnFileFound: TFileFoundEvent; | |
| 97 | + | FOnDirectoryFound: TDirectoryFoundEvent; | |
| 98 | + | FOnDirectoryEnter: TDirectoryEnterEvent; | |
| 99 | + | FFileAttribute: Word; | |
| 100 | + | FDirectoryAttribute: Word; | |
| 101 | + | procedure RaiseSearchingError; | |
| 102 | + | protected | |
| 103 | + | procedure DoDirectoryEnter; virtual; | |
| 104 | + | procedure DoDirectoryFound; virtual; | |
| 105 | + | procedure DoFileFound; virtual; | |
| 106 | + | public | |
| 107 | + | constructor Create; | |
| 108 | + | procedure Search(const ASearchPath: String; ASearchMask: String = ''; | |
| 109 | + | ASearchSubDirs: Boolean = True; CaseSensitive: Boolean = False); | |
| 110 | + | public | |
| 111 | + | property MaskSeparator: char read FMaskSeparator write FMaskSeparator; | |
| 112 | + | property FollowSymLink: Boolean read FFollowSymLink write FFollowSymLink; | |
| 113 | + | property FileAttribute: Word read FFileAttribute write FFileAttribute default faAnyfile; | |
| 114 | + | property DirectoryAttribute: Word read FDirectoryAttribute write FDirectoryAttribute default faDirectory; | |
| 115 | + | property OnDirectoryFound: TDirectoryFoundEvent read FOnDirectoryFound write FOnDirectoryFound; | |
| 116 | + | property OnFileFound: TFileFoundEvent read FOnFileFound write FOnFileFound; | |
| 117 | + | property OnDirectoryEnter: TDirectoryEnterEvent read FOnDirectoryEnter write FOnDirectoryEnter; | |
| 118 | + | end; | |
| 119 | + | ||
| 120 | + | function [[FindAllFiles]]( const SearchPath: String; SearchMask: String = ''; SearchSubDirs: Boolean = True): TStringList; | |
| 121 | + | function FindAllDirectories(const SearchPath: string; SearchSubDirs: Boolean = True): TStringList; | |
| 122 | + | ||
| 123 | + | //复制一个文件或一整个目录树 | |
| 124 | + | function [[CopyFile]](const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=[cffOverwriteFile]): boolean; | |
| 125 | + | function [[CopyFile]](const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean; | |
| 126 | + | function CopyDirTree(const SourceDir, TargetDir: string; Flags: TCopyFileFlags=[]): Boolean; | |
| 127 | + | ||
| 128 | + | //文件动作 | |
| 129 | + | function ReadFileToString(const Filename: string): string; | |
| 130 | + | function GetTempFilename(const Directory, Prefix: string): string; inline; | |
| 131 | + | ||
| 132 | + | //基本的函数,类似于RTL,但是使用UTF-8,而不是使用系统编码 | |
| 133 | + | //AnsiToUTF8和UTF8ToAnsi在Linux、BSD、MacOSX下需要一个widestring管理器 | |
| 134 | + | //但是通常情况下,这些操作系统使用UTF-8作为系统编码,因此不需要widestring管理器。 | |
| 135 | + | function NeedRTLAnsi: boolean; inline;// true,如果系统编码不是UTF-8 | |
| 136 | + | function NeedRTLAnsi: boolean; inline;// true,如果系统编码不是UTF-8 | |
| 137 | + | procedure SetNeedRTLAnsi(NewValue: boolean); inline; | |
| 138 | + | function UTF8ToSys(const s: string): string; inline;//像UTF8ToAnsi一样,但是更多的独立于widestring管理器 | |
| 139 | + | function SysToUTF8(const s: string): string; inline;//像AnsiToUTF8一样,但是更多的独立于widestring管理器 | |
| 140 | + | function ConsoleToUTF8(const s: string): string; inline;//转换OEM编码字符串到UTF8 (与一些Windows的特殊函数一起使用) | |
| 141 | + | function UTF8ToConsole(const s: string): string; inline;//转换UTF8字符串到控制台编码(由Write、WriteLn使用) | |
| 142 | + | ||
| 143 | + | // 文件操作 | |
| 144 | + | function FileExistsUTF8(const Filename: string): boolean; inline; | |
| 145 | + | function FileAgeUTF8(const FileName: string): Longint; inline; | |
| 146 | + | function DirectoryExistsUTF8(const Directory: string): Boolean; inline; | |
| 147 | + | function ExpandFileNameUTF8(const FileName: string): string; inline; | |
| 148 | + | function ExpandUNCFileNameUTF8(const FileName: string): string; | |
| 149 | + | function ExtractShortPathNameUTF8(Const FileName : String) : String; | |
| 150 | + | function FindFirstUTF8(const Path: string; Attr: Longint; out Rslt: TSearchRec): Longint; inline; | |
| 151 | + | function FindNextUTF8(var Rslt: TSearchRec): Longint; inline; | |
| 152 | + | procedure FindCloseUTF8(var F: TSearchrec); inline; | |
| 153 | + | function FileSetDateUTF8(const FileName: String; Age: Longint): Longint; inline; | |
| 154 | + | function FileGetAttrUTF8(const FileName: String): Longint; inline; | |
| 155 | + | function FileSetAttrUTF8(const Filename: String; Attr: longint): Longint; inline; | |
| 156 | + | function DeleteFileUTF8(const FileName: String): Boolean; inline; | |
| 157 | + | function RenameFileUTF8(const OldName, NewName: String): Boolean; inline; | |
| 158 | + | function FileSearchUTF8(const Name, DirList : String; ImplicitCurrentDir : Boolean = True): String; inline; | |
| 159 | + | function FileIsReadOnlyUTF8(const FileName: String): Boolean; inline; | |
| 160 | + | function GetCurrentDirUTF8: String; inline; | |
| 161 | + | function SetCurrentDirUTF8(const NewDir: String): Boolean; inline; | |
| 162 | + | function CreateDirUTF8(const NewDir: String): Boolean; inline; | |
| 163 | + | function RemoveDirUTF8(const Dir: String): Boolean; inline; | |
| 164 | + | function ForceDirectoriesUTF8(const Dir: string): Boolean; inline; | |
| 165 | + | function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle; inline; | |
| 166 | + | function FileCreateUTF8(Const FileName : string) : THandle; overload; inline; | |
| 167 | + | function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle; overload; inline; | |
| 168 | + | ||
| 169 | + | //环境 | |
| 170 | + | function ParamStrUTF8(Param: Integer): string; inline; | |
| 171 | + | function GetEnvironmentStringUTF8(Index: Integer): string; inline; | |
| 172 | + | function GetEnvironmentVariableUTF8(const EnvVar: string): String; inline; | |
| 173 | + | function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string; inline; | |
| 174 | + | function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false; CreateDir: boolean = false): string; inline; | |
| 175 | + | ||
| 176 | + | //其它 | |
| 177 | + | function SysErrorMessageUTF8(ErrorCode: Integer): String; inline; | |
| 178 | + | ``` | |
| 179 | + | ||
| 180 | + | ## 如何使用单元 | |
| 181 | + | ||
| 182 | + | 为在一个工程中启动FileUtil,只需要将LazUtils添加到所需要的软件包中。按照这个步骤: | |
| 183 | + | ||
| 184 | + | 1- 转到Lazarus IDE 菜单"工程 > 工程查看器"。 | |
| 185 | + | ||
| 186 | + | [](</File:FindAllFiles01.png>) [](</File:FindAllFiles01CN.png>) | |
| 187 | + | ||
| 188 | + | 2- 在"工程查看器"对话框窗口中,单击"添加 > 新建需要"。 | |
| 189 | + | ||
| 190 | + | [](</File:FindAllFiles02.png>) [](</File:FindAllFiles02CN.png>) | |
| 191 | + | ||
| 192 | + | 3- 在"新建需要"对话框窗口中,查找LazUtils单元,然后单击OK。 | |
| 193 | + | ||
| 194 | + | [](</File:FindAllFiles03.png>) [](</File:FindAllFiles03CN.png>) | |
| 195 | + | ||
| 196 | + | 4- 现在,你可以将FileUtil添加到一个Uses分句之中。 | |
| 197 | + | ||
| 198 | + | # 贡献者和更改 | |
| 199 | + | ||
| 200 | + | * 简体中文版本由 [ robsean](</User:Robsean> "User:Robsean") 于 2022-05-07 创建 | |
| 201 | + | ||
| 202 | + | --- | |
| 203 | + | *From the Free Pascal wiki, page "fileutil/zh CN", as the Internet Archive kept it on 2022-10-01. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
