知识大全 文件管理(三)[2]

Posted 文件

篇首语:常识是事物可能性的尺度,由预见和经验组成。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 文件管理(三)[2]相关的知识,希望对你有一定的参考价值。

DELPHI基础教程:文件管理(三)[2]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

   一致的界面

  文件拷贝 文件移动 文件更名以及后边的改变当前目录在形式上都表现为从一个源文件到一个目标文件 因而可以采用统一的用户界面 即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 = Move then Caption := Move

  else if Sender = Copy then Caption := Copy

  else if Sender = Rename then Caption := Rename

  else if Sender = ChangeDirectory 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] ) = 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;

   显示文件属性

  当程序执行Properties 菜单项的Click 事件处理过程时 首先弹出一个TFileAttrForm类型的对话框 显示文件的属性

  当用户修改并确认后程序重新设置文件属性

  Properties菜单项的Click事件处理过程如下

  procedure TFMForm Properties Click(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;

   其它文件管理功能的实现

  在子窗口的Function菜单中 定义了一些其它的文件管理功能

  ● Search :查找一个给定名字的文件 若存在则显示该文件属性

  ● Disk View :显示当前驱动器的大小和剩余空间

  ● View type :确定显示文件的类型

   文件查找

  当用户单击Search菜单项时 程序弹出一个对话框(如图 ) 要求输入待查找的文件名和查找路径 文件名可以是通配符 当用户确认后程序显示第一个匹配文件的属性(如图 ) 查找不到匹配文件则给出相应的信息

  在实现这一功能的最初设计中 我试图使用FileSearch函数 这个函数允许在多个不同路径中查找 但可惜的是 也许由于系统设计者的失误 这个函数并没有返回它应该返回的东西(第一个匹配文件的全路径名) 而是仍把输入的匹配符返回

cha138/Article/program/Delphi/201311/25233

相关参考