计算机等级考试二级Delphi辅导知识:文件管理_第7页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2012年12月27日
  procedure MoveFile(const FileName, DestName: TFileName);
   var
   Destination: TFileName;
   begin
   Destination := ExpandFileName(DestName);
   if not RenameFile(FileName, Destination) then
   begin
   if HasAttr(FileName, faReadOnly) then
   raise EFCantMove.Create(Format(SFCantMove, [FileName]));
   CopyFile(FileName, Destination);
   DeleteFile(FileName);
   end;
   end; 
   EFCanMove是一个自定义异常类: 
   type
   EFCanMove := Class(EStreamError);
   有关自定义异常类请参阅第十二章。
   文件删除、文件更名直接调用Delphi文件管理过程DeleteFile、RenameFile。它们都以文件名为参数。操作执行前应弹出一个对话框进行确认,执行完毕后应调用Update方法更新FileList的显示。 
   6.4.5.3 一致的界面 
   文件拷贝、文件移动、 文件更名以及后边的改变当前目录在形式上都表现为从一个源文件到一个目标文件。因而可以采用统一的用户界面,即ChangeForm对话框
   这四个菜单项共用一个Click事件处理过程,通过对Sender参数的检测,决定将要打开对话框的标题和显示内容。当用户按OK键关闭且目标文件(目录)非空时,程序弹出一个消息对话框要求用户进一步确认,而后执行相应的动作。
   共用的事件处理过程FileChange的程序清单如下: 
   procedure TFMForm.FileChange(Sender: TObject);
   var
   ChangeForm: TChangeForm;
   IsFile: Boolean;
   begin
   ChangeForm := TchangeForm.Create(Self);
   IsFile := True;
   with ChangeForm do
   begin
   if Sender = Move1 then Caption := 'Move'
   else if Sender = Copy1 then Caption := 'Copy'
   else if Sender = Rename1 then Caption := 'Rename'
   else if Sender = ChangeDirectory1 then
   begin
   Caption:='Change Directory';
   IsFile:=False;
   end
   else Exit;
   if IsFile then
   begin
   CurrentDir.Caption := FileList.Directory;
   FromFileName.Text := FileList.FileName;
   ToFileName.Text := '';
   end
   else
   begin
   CurrentDir.Caption := DriveTabSet.Tabs[DriveTabSet.TabIndex];
   FromFileName.Text := DirectoryOutline.Directory;
   ToFileName.Text := '';
   end;
   if (ShowModal <> idCancel) and (ToFileName.Text <> '') then
   ConfirmChange(Caption, FromFileName.Text, ToFileName.Text);
   end;
   end; 
   其中用到的自定义私有过程ConfirmChange用于执行相应的动作: 
   procedure TFMForm.ConfirmChange(const ACaption, FromFile, ToFile: String);
   begin
   if MessageDlg(Format('%s %s to %s', [ACaption, FromFile, ToFile]),
   mtConfirmation, [mbYes, mbNo], 0) = idYes then
   begin
   if ACaption = 'Move' then
   MoveFile(FromFile, ToFile)
   else if ACaption = 'Copy' then
   CopyFile(FromFile, ToFile)
   else if ACaption = 'Rename' then
   RenameFile(FromFile, ToFile)
   else if ACaption = 'Change Directory' then
   changeDirectory(ToFile);
   FileList.Update;
   end;
   end; 
   6.4.5.4 显示文件属性 
   当程序执行Properties 菜单项的Click 事件处理过程时,首先弹出一个TFileAttrForm类型的对话框,显示文件的属性
   当用户修改并确认后程序重新设置文件属性。
   Properties菜单项的Click事件处理过程如下: 
   procedure TFMForm.Properties1Click(Sender: TObject);
   var
   Attributes, NewAttributes: Word;
   FileAttrForm: TFileAttrForm;
   begin
   FileAttrForm := TFileAttrForm.Create(self);
   ShowFileAttr(FileAttrForm,FileList.FileName,FileList.Directory);
   end;
   其中过程ShowFileAttr的实现如下: 
   procedure TFMForm.ShowFileAttr(FileAttrForm:TFileAttrForm;
   AFileName,Directory:String);
   var
   Attributes,NewAttributes: Word;
   begin
   with FileAttrForm do
   begin
   FileName.Caption := AFileName;
   FilePath.Caption := Directory;
   ChangeDate.Caption := DateTimeToStr(FileDateTime(AFileName));
   Attributes := FileGetAttr(AFileName);
   ReadOnly.Checked := (Attributes and faReadOnly) = faReadOnly;
   Archive.Checked := (Attributes and faArchive) = faArchive;
   System.Checked := (Attributes and faSysFile) = faSysFile;
   Hidden.Checked := (Attributes and faHidden) = faHidden;
   if ShowModal <> idCancel then
   begin
   NewAttributes := Attributes;
   if ReadOnly.Checked then NewAttributes := NewAttributes or faReadOnly
   else NewAttributes := NewAttributes and not 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 faHidden;
   if NewAttributes <> Attributes then
   FileSetAttr(AFileName, NewAttributes);
   end;
   end;
   end; 
   以上过程中用到的函数FileDataTime在fmxutils单元中定义,返回一个TDatatime类型的变量。 
   function FileDateTime(const FileName: String): System.TDateTime;
   begin
   Result := FileDateToDateTime(FileAge(FileName));
   end; 
   6.4.6 其它文件管理功能的实现 
   在子窗口的Function菜单中,定义了一些其它的文件管理功能:
   ● Search :查找一个给定名字的文件,若存在则显示该文件属性
   ● Disk View :显示当前驱动器的大小和剩余空间
   ● View type :确定显示文件的类型 
   6.4.6.1 文件查找 
   当用户单击Search菜单项时,程序弹出一个对话框(如图6.10),要求输入待查找的文件名和查找路径。文件名可以是通配符。当用户确认后程序显示第一个匹配文件的属性(如图6.9)。查找不到匹配文件则给出相应的信息。
   在实现这一功能的最初设计中,我试图使用FileSearch函数,这个函数允许在多个不同路径中查找。但可惜的是:也许由于系统设计者的失误,这个函数并没有返回它应该返回的东西(第一个匹配文件的全路径名),而是仍把输入的匹配符返回。
   没有办法我只能再次使用FindFirst,这个函数的特性在6.3节中已进行了介绍。下面是这一功能的实现代码。 
   procedure TFMForm.search1Click(Sender: TObject);
   var
   SearchForm: TSearchForm;
   FileAttrForm: TFileAttrForm;
   FindIt,path: String;
   SearchRec: TSearchRec;
   Return: Integer;
   begin
   SearchForm := TSearchForm.Create(self);
   with SearchForm do
   begin
   SearchFile.text := '';
   SearchPath.text := DirectoryOutline.Directory;
   if (ShowModal <> idCancel) and
   (SearchFile.Text <> '') and (SearchPath.text <> '') then
   begin
   FindIt := SearchPath.text+'\'+SearchFile.text;
   Return := FindFirst(FindIt,faAnyFile,SearchRec);
   if Return <> 0 then
   FindIt := ''
   else
   FindIt := ExpandFileName(SearchRec.Name);
   end;
   if FindIt = '' then
   MessageDlg('Cannot find the file in current directory.',
   mtWarning, [mbOk], 0)
   else
   begin
   Path := ExtractFilePath(FindIt);
   FindIt := ExtractFileName(FindIt);
   FileAttrForm := TFileAttrForm.Create(self);
   ShowFileAttr(FileAttrForm,FindIt,Path);
   end;
   end;
   end; 
   6.4.6.2 显示磁盘信息
   当用户单击Disk View菜单项时,将弹出一个TDiskViewForm类型的对话框,用来显示当前磁盘的信息
   磁盘信息的获取是在DiskViewForm中DriveEdit编辑框的OnChange事件处理过程中实现的。 
   procedure TDiskViewForm.driveEditChange(Sender: TObject);
   var
   dr: Byte;
   Free,Total: LongInt;
   begin
   Free := DiskFree(0);
   Total := DiskSize(0);
   FreeSpace.text := IntToStr(Free)+ ' bytes.';
   TotalSpace.text := IntToStr(Total) + ' bytes.';
   end;
   DiskFree、DiskSize带参数为0表示当前驱动器。读者可以很容易把它改成按用户输入显示磁盘信息的情况。
   DiskViewForm中的三个编辑框设计时都令ReadOnly为True。 
   6.4.6.3 改变显示文件的类型 
   改变显示文件的类型事实上是设置FileList的Mask属性。我们利用一个标准的InputBox输入文件的匹配字符串。而后利用Update方法更新FileList。 
   procedure TFMForm.Viewtype1Click(Sender: TObject);
   var
   FileMask: String;
   begin
   FileMask := InputBox('File type','Input File type For View :',FileList.Mask);
   If FileMask = '' then FileMask := '*.*';
   FileList.Mask := FileMask;
   FileList.Update;
   CreateCaption;
   end;
   其中的CreateCaption私有过程将在(6.4.8)中进行介绍。 
   6.4.7 目录管理功能的实现 
   在子窗口的Directory菜单中,提供了目录管理功能:
   ● Create Directory :创建一个子目录
   ● Delete Directory :删除一个空的子目录
   ● Change Directory :改变当前目录

相关文章