Delphi 파일 조작에 관련된 함수

48564 단어 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

// 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(
' ! ' );
// :
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"notepad.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"notepad.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 ;

좋은 웹페이지 즐겨찾기