设为首页】 【加入收藏】 【网站地图】 【商品折扣
娱乐一生 娱乐明星
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
首页  |  java  |  .NET  |  C/C++  |  网页技术  |  php  |  asp  |  delphi  |  VC  |  VB开发  |  游戏开发  |  软件工程  |  Power Builder  |  Linux开发  |  Windows开发技巧
当前位置:首页 >> delphi >> Delphi例程-文件管理例程(1~15)_Delphi教程_src119.com

Delphi例程-文件管理例程(1~15)_Delphi教程_src119.com -


1.A ignFile 过程
 关联一个外部文件名到一个文件变量上。

单元
 ystem

语法
 rocedure A ignFile(var F; FileName: string);

描述
 调用A ignFile来初始化Delphi代码中的文件变量。F是一个任何文件类型的文件变量。FileName是一个字符串类型的表达式,或者,如果扩展的语法激活的话,是PChar类型的表达式。
 调用A ignFile之后,F就和外部文件关联起来直到F被关闭。所有在文件变量F上的更多的操作都会操作在名为Filename的外部文件上。
 当FileName参数为空时,A ignFile会将F和标准输入或标准输出文件关联起来。如果赋予一个空名字,在调用了Reset(F)之后,F将引用标准输入文件,而在调用了Rewrite(F)之后,F将引用标准输出文件。
 不要在已经打开的文件变量上使用A ignFile。
 注意:为了避免范围冲突,A ignFile 代替了在早期版本的Delphi产品中可用的A ign过程。然而为了向后兼容A ign仍然是可用的。
示例
var
  F: TextFile;
  S: string;
begin
  if OpenDialog1.Execute the  { Di lay Open dialog box }
  begin
  A ignFile(F, OpenDialog1.FileName); { File selected in dialog }
  Reset(F);
  Readln(F, S)  { Read first line of file }
  Edit1.Text :=   { Put string in a TEdit control }
  CloseFile(F);
  end;
end;

2.ChDir 过程
 改变当前目录

单元
 ystem

语法
 rocedure ChDir(co t S: string); overload;
 rocedure ChDir(P: PChar); overload;

描述
 ChDir 会将当前目录改变为由S或P指定的路径。如果这个操作失败,异常EInOutError将会引发。
 在Windows上,路径可以包含驱动器指示符(drive  ecifier),而这将导致当前的驱动盘同时改变。
 注意:在Delphi中,{$I+} 使用异常来处理运行时错误。当使用{$I-}时,要使用IOResult来检查I/O错误。
示例
begin
  {$I-}
  { Change to directory  ecified in Edit1 }
  ChDir(Edit1.Text);
  if IOResult < gt; 0 then
  Me ageDlg('Ca ot find directory', mtWarning, [mbOk], 0);
end;

3.CloseFile 过程
 终止文件变量和外部磁盘文件之间的关联

单元
 ystem

语法
 rocedure CloseFile(var F);

描述
 由于命名冲突,CloseFile代替了Close过程。使用CloseFile过程而不是Close来终止文件变量和外部磁盘文件之间的关联。
 F是一个使用Reset,Rewrite或A end打开的任何文件类型的文件变量。和F关联的外部文件会完全地更新然后关闭并释放文件句柄便于重用。
 注意:{$I+} 使用异常来处理运行时错误。当使用{$I-}时,要使用IOResult检查I/O 错误。

 4.CreateDir 函数
  创建一个新目录
单元
 ysUtils
语法
 function CreateDir(co t Dir: string): Boolea 
描述
 CreateDir 创建一个新目录。如果新目录成功创建,则返回值为true,或者如果出现错误则返回值为false。
示例
 下面的例子会创建目录'C:\temp',如果目录不存在的话。

uses FileCtrl;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not DirectoryExists('c:\temp') then
  if not CreateDir('C:\temp') then
  raise Exception.Create('Ca ot create c:\temp');
end;

5.DeleteFile 函数
 从删除一个磁盘文件
单元
 ysUtils
语法
 function DeleteFile(co t FileName: string): Boolea 
描述
 DeleteFile 删除磁盘上由 FileName 命名的文件。如果文件不能被删除或者文件不存在,函数将返回false。
示例
 
if FileExists(FileName) then
  if Me ageDlg('Do you really want to delete ' + ExtractFileName(FileName) + '?'), mtConfirmation, [mbYes, m o], 0, m o) = IDYes then
  DeleteFile(FileName);

6.DirectoryExists 函数
 确定指定的目录是否存在
单元
 ysUtils
语法
 function DirectoryExists(co t Directory: string): Boolea 
描述
 调用 DirectoryExists 来确定由Directory参数指定的目录是否存在。如果目录存在,函数返回true。如果目录不存在,函数返回false。
 如果输入的是全称路径名(full path name),DirectoryExists 会沿着指定的路径查找目录。否则Directory参数会被认为是当前目录的相对路径。
 FileCtrl 单元(仅用于Windows) 同样包含一个 DirectoryExists 函数。然而,FileCtrl 版本是不赞成的,SysUtils 版本是首选的,即使代码不需要跨平台(However, the FileCtrl version is deprecated, and the SysUtils version preferred, even if the code does not need to be cro -platform)。

7.DiskFree 函数
 返回指定盘符上空闲的字节数
单元
 ysUtils
语法
 function DiskFree(Drive: Byte): Int64;
描述
 DiskFree 返回指定驱动盘()的空闲字节数,其中 0 = 当前盘, 1 = A, 2 = B,等等。如果驱动盘数字无效,DiskFree 返回-1。
 注意:DiskFree 仅在Windows上可用。
示例
var
  S: string;
  AmtFree: Int64;
  Total:  Int64;
begin
  AmtFree := DiskFree(0);
  Total := DiskSize(0);
  S := IntToStr(AmtFree div Total) + 'percent of the  ace on drive 0 is free: ' (AmtFree div 1024) + ' Kbytes free. ';
  Label1.Caption :=  
end;

8.DiskSize 函数
 返回指定盘符的字节大小
单元
 ysUtils
语法
 function DiskSize(Drive: Byte): Int64;
描述
 DiskSize 返回指定驱动盘的字节大小,其中 0 = 当前盘,1 = A, 2 = B, 等等。如果驱动盘数字无效,DiskSize返回-1。
 注意:DiskSize 仅在Windows上可用。

9.文件模式常量(File mode co tants)
 文件模式常量用于打开和关闭磁盘文件
单元
 ystem
语法
 co t fmClosed = $D7B0  // closed file
 co t fmI ut  = $D7B1  // reset file (TTextRec)
 co t fmOutput = $D7B2  // rewritten file (TTextRec)
 co t fmInOut  = $D7B3  // reset or rewritten file (TFileRec)
 co t fmCRLF  = $8  // DOS-style EoL and EoF markers (TTextRec)
 co t fmMask  = $D7B3  // mask out fmCRLF flag (TTextRec)
描述 当打开和关闭磁盘文件时,使用文件模式常量。这些常量主要用在这样的Delphi代码中,TFileRec和TTextRec的Mode字段包含这些值中的某个值(These co tants are used primarily in Delphi code, where the Mode field of TFileRec and TTextRec contain one of these values.)。

10.文件名称常量(File name co tants) 
 文件名称常量用于以平台中立的方式表达文件名称。
单元
 ysUtils
语法
  co t
 athDelim  = {$IFDEF MSWINDOWS} '\'; {$ELSE} '/'; {$ENDIF}
 DriveDelim = {$IFDEF MSWINDOWS} ':'; {$ELSE} ''  {$ENDIF}
 athSe  = {$IFDEF MSWINDOWS} ';'; {$ELSE} ':'; {$ENDIF}
描述
 文件名称常量指定了在Windows和Linux中不同的的定界符和分隔符(delimiter and separator)。

11.文件打开模式常量(File open mode co tants) 
 打开打开模式常量用于控制对文件或流的访问模式。
单元
 ysUtils
语法
On Windows:
  co t
  fmCreate  = $FFFF;
  fmOpenRead  = $0000;
  fmOpenWrite  = $0001;
  fmOpenReadWrite  = $0002;

  fmShareCompat  = $0000 platform;
  fmShareExclusive = $0010;
  fmShareDenyWrite = $0020;
  fmShareDenyRead  = $0030 platform;
  fmShareDenyNone  = $0040;

On Linux:
  co t
  fmOpenRead  = O_RDONLY;
  fmOpenWrite  = O_WRONLY;
  fmOpenReadWrite  = O_RDWR;
  fmShareExclusive = $0010;
  fmShareDenyWrite = $0020;
  fmShareDenyNone  = $0030;

描述
 当文件或流被打开时,文件打开模式常量用于控制文件或流能够如何共享。
 TFileStream构造函数有一个Mode参数,你能够设置为这些常量中的一个:
Co tant Definition

fmCreate 如果文件存在,那么将打开用于写访问,否则会创建新文件。其他的常量都声明在 SysUtils 单元,而这个常量声明在 Cla es 单元中
fmOpenRead 仅以读访问方式打开
fmOpenWrite 仅以写访问方式打开
fmOpenReadWrite 以读写访问方式打开
fmShareCompat 和FCB打开的方法兼容。不要在跨平台应用程序中使用这个模式
fmShareExclusive 读写访问被拒绝
fmShareDenyWrite 写访问被拒绝
fmShareDenyRead 读访问被拒绝。不要在跨平台应用程序中使用这个模式
fmShareDenyNone 允许其他代码进行完全的访问

12.FileAcce Rights 变量 
 当应用程序被调用时指向特殊的命令行参数。
单元
 ystem
语法
 var FileAcce Rights: Integer platform;
描述
 在 Windows 中,FileAcce Rights 变量被忽略。
 在 Linux 中,每个文件都有一组许可位(permi ion bits)控制着对文件的访问。当创建新文件时,FileAcce Rights 指定了一组默认的许可标记来使用。当你没有显式指定要使用的许可位时,FileCreate 方法使用 FileAcce Rights 来设置它创建的文件的访问权力。
13.FileAge 函数 
 返回文件的OS时间戳(Retur  the OS timestamp of a file.)
单元
 ysUtils
语法
 function FileAge(co t FileName: string): Integer;
描述
 调用 FileAge 来获得由 FileNameto 指定的文件的 OS 时间戳。返回值可以使用 FileDateToDateTime函数转换为TDateTime对象。如果文件不存在返回值为 -1。
 在Linux中,-1 是一个有效的时间戳。可使用 FileExists 核实文件不存在。
示例
 下面的代码将一个文件的属性读入一组变量中,并在文件属性对话框中设置检查框以表现当前的属性,然后执行对话框。如果用户改变并接受对话框的设置,代码将设置文件属性以匹配改变的设置:
procedure TFMForm.Properties1Click(Sender: TObject);
var
  Attributes, NewAttributes: Word;
begin
  with FileAttrForm do
  begin
  FileDirName.Caption := FileList.Items[FileList.ItemIndex];
  { set box caption }
  PathName.Caption := FileList.Directory;
  { show directory name }
  ChangeDate.Caption :=
  DateTimeToStr(FileDateToDateTime(FileAge(FileList.FileName)));
  Attributes := FileGetAttr(FileDirName.Caption);
  { read file attributes }
  ReadOnly.Checked := (Attributes and SysUtils.faReadOnly) = faReadOnly;
  Archive.Checked := (Attributes and faArchive) = faArchive;
  System.Checked := (Attributes and faSysFile) = faSysFile;
  Hidden.Checked := (Attributes and faHidden) = faHidde 
  if ShowModal < gt; id_Cancel the { execute dialog box }
  begin
  NewAttributes := Attribute 
  { start with original attributes }
  if ReadOnly.Checked then
  NewAttributes := NewAttributes or SysUtils.faReadOnly
  else
  NewAttributes := NewAttributes and not SysUtils.faReadOnly;
  if Archive.Checked then
  NewAttributes := NewAttributes or faArchive
  else
  NewAttributes := NewAttributes and not faArchive;
  if System.Checked then
  NewAttributes := NewAttributes or faSysFile
  else
  NewAttributes := NewAttributes and not faSysFile;
  if Hidden.Checked then
  NewAttributes := NewAttributes or faHidden
  else
  NewAttributes := NewAttributes and not faHidde 
  if NewAttributes < gt; Attributes then { if anything changed... }
  FileSetAttr(FileDirName.Caption, NewAttributes);
  { ...write the new values }
  end;
  end;
end;
 
14.FileClose 过程 
 关闭指定的文件
单元
 ysUtils
语法
 rocedure FileClose(Handle: Integer);
描述
 FileClose 关闭给定文件句柄的文件。句柄在文件使用FileOpen或FileCreate打开时获得。
 当和Delphi语言的文件变量一起使用时,应使用 CloseFile 过程代替。
示例
 下面的例子使用了一个按钮,一个字符串栅格和一个保存对话框在窗体上。单击按钮后,用户将被提示输入文件名。当用户单OK后,字符串栅格的内容将被写到指定的文件中。附加的信息同样被写到文件中,以便文件能够使用FileRead函数容易地读取。
procedure TForm1.Button1Click(Sender: TObject);
var
  Backu ame: string;
  FileHandle: Integer;
  StringLen: Integer;
  X: Integer;
  Y: Integer;
begin
  if SaveDialog1.Execute then
  begin
  if FileExists(SaveDialog1.FileName) then
  begin
  Backu ame := ExtractFileName(SaveDialog1.FileName);
  Backu ame := ChangeFileExt(Backu ame, '.BAK');
  if not RenameFile(SaveDialog1.FileName, Backu ame) then
  raise Exception.Create('Unable to create backup file.');
  end;
  FileHandle := FileCreate(SaveDialog1.FileName);
  { Write out the number of rows and colum  in the grid. }
  FileWrite(FileHandle,
  StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
  FileWrite(FileHandle,
  StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
  for X := 0 to StringGrid1.ColCount - 1 do
  begin
  for Y := 0 to StringGrid1.RowCount - 1 do
  begin
  { Write out the length of each string, followed by the string itself. }
  StringLen := Length(StringGrid1.Cells[X,Y]);
  FileWrite(FileHandle, StringLen, SizeOf(StringLen));
  FileWrite(FileHandle,
  StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
  end;
  end;
  FileClose(FileHandle);
  end;
end;

15.FileCreate 函数 
 创建一个新文件
单元
 ysUtils
语法
 function FileCreate(co t FileName: string): Integer; overload;
 function FileCreate(co t FileName: string; Rights: Integer): Integer; overload;
描述
 FileCreate 用指定的名称创建新文件。如果返回值是正数,说明函数成功而且值是新文件的句柄。返回值是-1说明有错误发生。
 在 Windows中,FileAcce Rights 变量和 Rights 参数被忽略。



 

娱乐图摘

更多 >>

靓丽清纯美女meimei

美女私房全裸照
导演劝女演员脱衣服(视频)

大胆火辣人体艺术写真(图)

黑丝妹妹热辣诱惑-丝袜美女妹妹

PLMM 漂亮妹妹图集-妹妹图库

全球美女图库-美女集中营

52MM 我爱漂亮妹妹-制服妹妹诱惑

图王图库-世界美女明星图片资料库
美女写真集锦

激情两性-解密性生活
浴室MM湿身内衣诱惑
邻家小妹洗澡被偷拍(视频)

热点文章

更多

· 使用MIDAS访问远程Access数据库_Delphi教程_
· Delphi2005学习笔记4——再谈NameSpace和D
· 多层数据库开发十二:使用数据控件_Delphi教程_src1
· 在Delphi中自己建立交叉表_Delphi教程_src11
· 用Delphi设计代理服务器_Delphi教程_src119
· Delphi开发单机瘦数据库程序要点_Delphi教程_sr
· 拖拉打开文件_Delphi教程_src119.com
· 搜索字符串在流中的位置_Delphi教程_src119.co
· 用Delphi编写Win2000服务程序_Delphi教程_
· 創建一個簡單的"專家"_Delphi教程

热点文章

更多