//判断文件是否存在 FileExists //判断文件夹是否存在 DirectoryExists //删除文件 DeleteFile; Windows.DeleteFile //删除文件夹 RemoveDir; RemoveDirectory //获取当前文件夹 GetCurrentDir //设置当前文件夹 SetCurrentDir; ChDir; SetCurrentDirectory //获取指定驱动器的当前路径名 GetDir //文件改名 RenameFile //建立文件夹 CreateDir; CreateDirectory; ForceDirectories //删除空文件夹 RemoveDir; RemoveDirectory //建立新文件 FileCreate //获取当前文件的版本号 GetFileVersion //获取磁盘空间 DiskSize; DiskFree //搜索文件 FindFirst; FindNext; FindClose //读取与设置文件属性 FileGetAttr; FileSetAttr //获取文件的创建时间 FileAge; FileDateToDateTime
Delphi代码
-
-
var
-
f: string;
-
begin
-
f := 'c:\temp\test.txt';
-
if not FileExists(f) then
-
begin
-
-
end;
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
dir: string;
-
begin
-
dir := 'c:\temp';
-
if not DirectoryExists(dir) then
-
begin
-
-
end;
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
f: string;
-
begin
-
f := 'c:\temp\test.txt';
-
-
-
-
Windows.DeleteFile(PChar(f));
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
dir: string;
-
begin
-
dir := 'c:\temp';
-
RemoveDir(dir);
-
-
-
RemoveDirectory(PChar(dir));
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
dir: string;
-
begin
- dir := GetCurrentDir;
-
ShowMessage(dir);
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
dir: string;
-
begin
-
dir := 'c:\temp';
-
if SetCurrentDir(dir) then
-
ShowMessage(GetCurrentDir);
-
-
-
ChDir(dir);
-
-
-
SetCurrentDirectory(PChar(Dir));
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
dir: string;
- b: Byte;
-
begin
-
b := 0;
- GetDir(b,dir);
-
ShowMessage(dir);
-
-
-
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
OldName,NewName: string;
-
begin
-
OldName := 'c:\temp\Old.txt';
-
NewName := 'c:\temp\New.txt';
-
-
if RenameFile(OldName,NewName) then
-
ShowMessage('改名成功!');
-
-
-
SetCurrentDir('c:\temp');
-
OldName := 'Old.txt';
-
NewName := 'New.txt';
-
-
if RenameFile(OldName,NewName) then
-
ShowMessage('改名成功!');
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
dir: string;
-
begin
-
dir := 'c:\temp\delphi';
-
if not DirectoryExists(dir) then
-
CreateDir(dir);
-
-
-
CreateDirectory(PChar(dir),nil);
-
-
-
dir := 'c:\temp\CodeGear\Delphi\2007\万一';
-
ForceDirectories(dir);
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
dir: string;
-
begin
-
dir := 'c:\temp\delphi';
-
RemoveDir(dir);
-
-
-
RemoveDirectory(PChar(dir));
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
FileName: string;
- i: Integer;
-
begin
-
FileName := 'c:\temp\test.dat';
- i := FileCreate(FileName);
-
-
if i>0 then
-
ShowMessage('新文件的句柄是: ' + IntToStr(i))
-
else
-
ShowMessage('创建失败!');
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
s: string;
- i: Integer;
-
begin
-
s := 'C:\WINDOWS\notepad.exe';
-
i := GetFileVersion(s);
-
ShowMessage(IntToStr(i));
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
- r: Real;
-
s: string;
-
begin
-
r := DiskSize(3);
-
r := r/1024/1024/1024;
-
Str(r:0:2,s);
-
s := 'C盘总空间是: ' + s + ' GB';
-
ShowMessage(s);
-
-
r := DiskFree(3);
-
r := r/1024/1024/1024;
-
Str(r:0:2,s);
-
s := 'C盘可用空间是: ' + s + ' GB';
-
ShowMessage(s);
-
end;
-
-
-
var
-
FileName,Dir,s: string;
-
begin
-
FileName := 'notepad.exe';
-
Dir := 'c:\windows';
- s := FileSearch(FileName,Dir);
-
-
if s<>'' then
-
ShowMessage(s)
-
else
-
ShowMessage('没找到');
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
sr: TSearchRec;
-
Attr: Integer;
-
s: string;
-
List: TStringList;
-
begin
-
s := 'c:\windows\*.txt';
-
Attr := faAnyFile;
-
List := TStringList.Create;
-
-
if FindFirst(s,Attr,sr)=0 then
-
begin
-
repeat
-
List.Add(sr.Name);
-
until(FindNext(sr)<>0);
-
end;
-
FindClose(sr);
-
-
ShowMessage(List.Text);
-
List.Free;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
FileName: string;
-
Attr: Integer;
-
begin
-
FileName := 'c:\temp\Test.txt';
- Attr := FileGetAttr(FileName);
-
ShowMessage(IntToStr(Attr));
-
-
-
Attr := FILE_ATTRIBUTE_READONLY or FILE_ATTRIBUTE_HIDDEN;
-
if FileSetAttr(FileName,Attr)=0 then
-
ShowMessage('设置成功!');
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
end;
-
- --------------------------------------------------------------------------------
-
-
-
-
var
-
FileName: string;
- ti: Integer;
- dt: TDateTime;
-
begin
-
FileName := 'c:\temp\Test.txt';
- ti := FileAge(FileName);
-
ShowMessage(IntToStr(ti));
-
-
dt := FileDateToDateTime(ti);
-
ShowMessage(DateTimeToStr(dt));
-
end;
|
请发表评论