Delphi 파일 조작의 일부 함수
8589 단어 Delphi
// 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
// FileExists
var
f: string;
begin
f := 'c:\temp\test.txt';
if not FileExists(f) then
begin
//
end;
end;
--------------------------------------------------------------------------------
// DirectoryExists
var
dir: string;
begin
dir := 'c:\temp';
if not DirectoryExists(dir) then
begin
//
end;
end;
--------------------------------------------------------------------------------
// DeleteFile; Windows.DeleteFile
var
f: string;
begin
f := 'c:\temp\test.txt';
//DeleteFile(f); // Boolean
// API:
Windows.DeleteFile(PChar(f)); // Boolean
end;
--------------------------------------------------------------------------------
// RemoveDir; RemoveDirectory
var
dir: string;
begin
dir := 'c:\temp';
RemoveDir(dir); // Boolean
// API:
RemoveDirectory(PChar(dir)); // Boolean
end;
--------------------------------------------------------------------------------
// GetCurrentDir
var
dir: string;
begin
dir := GetCurrentDir;
ShowMessage(dir); //C:\Projects
end;
--------------------------------------------------------------------------------
// SetCurrentDir; ChDir; SetCurrentDirectory
var
dir: string;
begin
dir := 'c:\temp';
if SetCurrentDir(dir) then
ShowMessage(GetCurrentDir); //c:\temp
//
ChDir(dir); //
// API:
SetCurrentDirectory(PChar(Dir)); // Boolean
end;
--------------------------------------------------------------------------------
// GetDir
var
dir: string;
b: Byte;
begin
b := 0;
GetDir(b,dir);
ShowMessage(dir); //
// : 1、2、3、4... : A、B、C、D...
//0
end;
--------------------------------------------------------------------------------
// RenameFile
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;
--------------------------------------------------------------------------------
// CreateDir; CreateDirectory; ForceDirectories
var
dir: string;
begin
dir := 'c:\temp\delphi';
if not DirectoryExists(dir) then
CreateDir(dir); // Boolean
// API:
CreateDirectory(PChar(dir),nil); // Boolean
// :
dir := 'c:\temp\CodeGear\Delphi\2007\ ';
ForceDirectories(dir); // Boolean
end;
--------------------------------------------------------------------------------
// RemoveDir; RemoveDirectory
var
dir: string;
begin
dir := 'c:\temp\delphi';
RemoveDir(dir); // Boolean
// API:
RemoveDirectory(PChar(dir)); // Boolean
end;
--------------------------------------------------------------------------------
// FileCreate
var
FileName: string;
i: Integer;
begin
FileName := 'c:\temp\test.dat';
i := FileCreate(FileName);
if i>0 then
ShowMessage(' : ' + IntToStr(i))
else
ShowMessage(' !');
end;
--------------------------------------------------------------------------------
// GetFileVersion
var
s: string;
i: Integer;
begin
s := 'C:\WINDOWS
otepad.exe';
i := GetFileVersion(s); // -1
ShowMessage(IntToStr(i)); //327681 ( )
end;
--------------------------------------------------------------------------------
// DiskSize; DiskFree
var
r: Real;
s: string;
begin
r := DiskSize(3); // C: ,
r := r/1024/1024/1024;
Str(r:0:2,s); //
s := 'C : ' + s + ' GB';
ShowMessage(s); //xx.xx GB
r := DiskFree(3); // C:
r := r/1024/1024/1024;
Str(r:0:2,s);
s := 'C : ' + s + ' GB';
ShowMessage(s); //xx.xx GB
end;
// FileSearch
var
FileName,Dir,s: string;
begin
FileName := 'notepad.exe';
Dir := 'c:\windows';
s := FileSearch(FileName,Dir);
if s<>'' then
ShowMessage(s) //c:\windows
otepad.exe
else
ShowMessage(' ');
end;
--------------------------------------------------------------------------------
// FindFirst; FindNext; FindClose
var
sr: TSearchRec; // TSearchRec
Attr: Integer; //
s: string; //
List: TStringList; //
begin
s := 'c:\windows\*.txt';
Attr := faAnyFile; // faAnyFile
List := TStringList.Create; //List
if FindFirst(s,Attr,sr)=0 then // , sr , 0
begin
repeat //
List.Add(sr.Name); // List
until(FindNext(sr)<>0); // sr , FindNext , 0
end;
FindClose(sr); // ,
ShowMessage(List.Text); //
List.Free; // List
// :
//TSearchRec 、 、
//TSearchRec , :
//faReadOnly 1
//faHidden 2
//faSysFile 4
//faVolumeID 8
//faDirectory 16
//faArchive 32
//faSymLink 64
//faAnyFile 63
//s ? , 7 ?, *, : C:\*
// repeat ,
end;
--------------------------------------------------------------------------------
// FileGetAttr; FileSetAttr
var
FileName: string;
Attr: Integer; //
begin
FileName := 'c:\temp\Test.txt';
Attr := FileGetAttr(FileName);
ShowMessage(IntToStr(Attr)); //32,
// :
Attr := FILE_ATTRIBUTE_READONLY or FILE_ATTRIBUTE_HIDDEN;
if FileSetAttr(FileName,Attr)=0 then // 0
ShowMessage(' !');
// ( ):
//FILE_ATTRIBUTE_READONLY = 1;
//FILE_ATTRIBUTE_HIDDEN = 2;
//FILE_ATTRIBUTE_SYSTEM = 4;
//FILE_ATTRIBUTE_DIRECTORY = 16
//FILE_ATTRIBUTE_ARCHIVE = 32;
//FILE_ATTRIBUTE_DEVICE = 64
//FILE_ATTRIBUTE_NORMAL = 128;
//FILE_ATTRIBUTE_TEMPORARY = 256
//FILE_ATTRIBUTE_SPARSE_FILE = 512
//FILE_ATTRIBUTE_REPARSE_POINT = 1204
//FILE_ATTRIBUTE_COMPRESSED = 2048;
//FILE_ATTRIBUTE_OFFLINE = 4096
//FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192;
//FILE_ATTRIBUTE_ENCRYPTED = 16384
end;
--------------------------------------------------------------------------------
// FileAge; FileDateToDateTime
var
FileName: string;
ti: Integer;
dt: TDateTime;
begin
FileName := 'c:\temp\Test.txt';
ti := FileAge(FileName);
ShowMessage(IntToStr(ti)); // : 931951472,
dt := FileDateToDateTime(ti); //
ShowMessage(DateTimeToStr(dt)); //2007-12-12 14:27:32
end;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Delphi] TStringBuilder그리고 꼭 사용해야만 할까? 그림처럼 Heap 영역에 "Hello" 공간을 생성하고 포인팅을 한다. "Hello World" 공간을 새로 생성한 후 포인팅을 하게 된다. 결국 "Hello" 라는 String 객체가 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.