Delphi 文件处理(2)
1. 文件管理类函数
判断文件是否存在 FileExists 判断文件夹是否存在 DirectoryExists
删除文件 DeleteFile; Windows.DeleteFile 删除文件夹 RemoveDir; RemoveDirectory
获取当前文件夹 GetCurrentDir 设置当前文件夹 SetCurrentDir; ChDir; SetCurrentDirectory
获取指定驱动器的当前路径名 GetDir 文件改名 RenameFile
建立文件夹 CreateDir; CreateDirectory; ForceDirectories 删除空文件夹 RemoveDir; RemoveDirectory
获取当前文件的版本号 GetFileVersion 获取磁盘空间 DiskSize; DiskFree
查找一个文件 FileSearch 搜索文件 FindFirst; FindNext; FindClose
读取与设置文件属性 FileGetAttr; FileSetAttr 获取文件的创建时间 FileAge; FileDateToDateTime
1.1 API 文件处理函数
1. GetWindowsDirectory - 获取 Windows 所在目录
//声明:GetWindowsDirectory(
lpBuffer: PChar;{缓冲区}
uSize: UINT {缓冲区大小}): UINT; {返回实际长度}
num := GetWindowsDirectory(arr, MAX_PATH);
2. GetSystemDirectory - 返回 System 文件夹路径
//声明:
GetSystemDirectory(
lpBuffer: PChar; {缓冲区}
uSize: UINT {缓冲区大小}
): UINT; {返回实际长度}
num := GetSystemDirectory(arr, MAX_PATH);
3.GetTempPath - 获取临时文件夹路径
//声明:
GetTempPath(
nBufferLength: DWORD; {缓冲区大小}
lpBuffer: PChar {缓冲区}
): DWORD; {返回实际长度}
num := GetTempPath(MAX_PATH, arr)
4.GetTempFileName - 生成一个临时文件名
5.CopyFile - 复制文件
//声明:
CopyFile(
lpExistingFileName: PChar; {源文件}
lpNewFileName: PChar; {目标文件}
bFailIfExists: BOOL {如果目标文件存在, True: 失败; False: 覆盖}
): BOOL;
6.CreateDirectory - 建立文件夹
CreateDirectory(PChar(dir), nil);
7.CreateDirectoryEx - 根据模版建立文件夹
CreateDirectoryEx(PChar(TDir), PChar(Dir),nil);
8.RemoveDirectory - 删除空目录//声明:
RemoveDirectory(
lpPathName: PAnsiChar {目录名}
): BOOL;
if RemoveDirectory(PChar(Dir))then
9.SetCurrentDirectory、GetCurrentDirectory - 设置与获取当前目录
SetCurrentDirectory('c:\temp');
GetCurrentDirectory(SizeOf(buf), buf);
10.SetVolumeLabel - 设置磁盘卷标
SetVolumeLabel('c:\', 'NewLabel');
2. INI 文件读取
[delphi] view plaincopyprint?private
List: TStrings;
Fini: TIniFile;
Path: string;
Section,Key: string;
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses
ShellAPI;
{$R *.dfm}
{
INI File ===============================================================
}
procedure TMainForm.FormCreate(Sender: TObject);
begin
Path := ChangeFileExt(ParamStr(0),'.ini');
Fini := TIniFile.Create(Path);
end;
//写入 Write INI
procedure TMainForm.Button1Click(Sender: TObject);
begin
Section := 'Delphi xe Title';
Key := 'Astring Key';
Fini.WriteString(Section,Key,'a_string value');
ShellExecute(0,'open',PAnsiChar(Path),nil,nil,sw_show);
end;
//读取 Read INI
procedure TMainForm.Button2Click(Sender: TObject);
var
s: string;
begin
s := Fini.ReadString('Delphi xe Title','Astring Key','');
ShowMessage(s);
end;
//读取 字节Read ReadSections ....Section
procedure TMainForm.Button3Click(Sender: TObject);
begin
List := TStringList.Create;
Fini.ReadSections(List);
ShowMessage(List.Text); //Delphi xe Title
end;
//读取关键字 Read ReadSection ...Key
procedure TMainForm.Button4Click(Sender: TObject);
begin
List := TStringList.Create;
Fini.ReadSection('Delphi xe Title',List);
ShowMessage(List.Text); //AString Key
end;
//删除节点DeleteKey
procedure TMainForm.Button5Click(Sender: TObject);
begin
Fini.DeleteKey('Delphi xe Title','AString Key');
end;
//删除全部 EraseSection
procedure TMainForm.Button6Click(Sender: TObject);
begin
Fini.EraseSection('Delphi xe Title');
end;
//Other...
procedure TMainForm.Button7Click(Sender: TObject);
var
b: Boolean;
s: string;
begin
b := Fini.SectionExists('Delphi xe Title'); {读取 小节}
ShowMessage(BoolToStr(b));
b := Fini.ValueExists('Delphi xe Title','AString Key'); {读取}
ShowMessage(BoolToStr(b));
s := Fini.FileName; {文件名}
ShowMessage(s);
end;
private
List: TStrings;
Fini: TIniFile;
Path: string;
Section,Key: string;
{ Private de
补充:软件开发 , Delphi ,