首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 檔案管理核心操作:統一介面處理複製、移動、更名與屬性編輯

     編輯:Delphi

統一介面設計

檔案複製、移動、更名、變更目錄皆屬「來源→目標」操作,共用 TChangeForm 對話框與 FileChange 事件處理器。依 Sender 判斷操作類型,設定對話框標題與初始值:

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; ToFileName.Text := ''; end
    else begin CurrentDir.Caption := DriveTabSet.Tabs[DriveTabSet.TabIndex]; ToFileName.Text := ''; end;
    if (ShowModal <> idCancel) and (ToFileName.Text <> '') then
      ConfirmChange(Caption, FromFileName.Text, ToFileName.Text);
  end;
end;

確認執行核心邏輯

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;

檔案屬性編輯

Properties 選單彈出 TFileAttrForm,讀取/設定屬性位元(faReadOnly/faArchive/faSysFile/faHidden):

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;

FileDateTime 封裝於 fmxutils:Result := FileDateToDateTime(FileAge(FileName))。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved