程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi 文件操作的封裝

Delphi 文件操作的封裝

編輯:Delphi

 Unit Name: FileOper

 Author:    liubin
 Date:      2010-12-07
 Purpose:   用於文件及文件夾操作
 History:
-----------------------------------------------------------------------------}
unit FileOper;

interface
uses Windows,shellapi,SysUtils;

type
  TFileOper =class
    public
      class  function WinErasefile(Owner: Integer; WichFiles: string; SendToRecycleBin, Confirm: Boolean): Boolean;
      class  function WinErasepath(Owner: Integer; WichFiles: string; SendToRecycleBin, Confirm: Boolean): Boolean;
      class  function WinMovepath(Owner:Integer;FromFile, Tofile:string;ReNameOnCollision, Confirm:Boolean):Boolean;
      class  function WinMovefile(Owner:Integer;FromFile, Tofile:string;ReNameOnCollision, Confirm:Boolean):Boolean;
      class  function WinCopypath(Owner: Integer; FromFile, Tofile: string;ReNameOnCollision, Confirm: Boolean): Boolean;
      class  function WinCopyfile(Owner: Integer; FromFile, Tofile: string;ReNameOnCollision, Confirm: Boolean): Boolean;
      class  function IsFileInUse(fName: string): boolean;
  end;

implementation

class function TFileOper.IsFileInUse(fName: string): boolean;
var
  HFileRes: HFILE;
begin
   Result := false;
   if not FileExists(fName) then //如果文件不存在
     exit;
   HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,
     0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
   Result := (HFileRes = INVALID_HANDLE_VALUE);
   if not Result then
     CloseHandle(HFileRes);
end;
class function TFileOper.WinErasefile(Owner: Integer; WichFiles: string; SendToRecycleBin, Confirm: Boolean): Boolean;
//用於將文件直接刪除或移動到回收站
var
  Struct : TSHFileOpStructA;
begin
  FillChar(Struct, SizeOf(Struct), 0);
  While pos(';', WichFiles)>0 do
  WichFiles[pos(';', WichFiles)] := #0;
  WichFiles := WichFiles + #0#0;
  with Struct do
  begin
  wnd := Owner;
  wFunc := FO_Delete;
  pFrom := PChar(WichFiles);
  pTo := nil;
  If not Confirm then fFlags := FOF_NOCONFIRMATION;
  If SendToRecycleBin then fFLags := fFlags or FOF_ALLOWUNDO or FOF_FILESONLY
  else fFlags := fFlags or 0 or FOF_FILESONLY;
  hNameMappings := nil;
  lpszProgressTitle := nil;
  end;
  result := (SHFileOperationA(Struct)=0) and (not Struct.fAnyOperationsAborted);
end;

 

class function TFileOper.WinErasepath(Owner: Integer; WichFiles: string; SendToRecycleBin, Confirm: Boolean): Boolean;
//用於將目錄直接刪除或移動到回收站
var
  Struct : TSHFileOpStructA;
begin
  FillChar(Struct, SizeOf(Struct), 0);
  While pos(';', WichFiles)>0 do
  WichFiles[pos(';', WichFiles)] := #0;
  WichFiles := WichFiles + #0#0;
  with Struct do
  begin
  wnd := Owner;
  wFunc := FO_Delete;
  pFrom := PChar(WichFiles);
  pTo := nil;
  If not Confirm then fFlags := FOF_NOCONFIRMATION;
  If SendToRecycleBin then fFLags := fFlags or FOF_ALLOWUNDO
  else fFlags := fFlags or 0 or FOF_FILESONLY;
  hNameMappings := nil;
  lpszProgressTitle := nil;
  end;
  result := (SHFileOperationA(Struct)=0) and (not Struct.fAnyOperationsAborted);
end;


class  function TFileOper.WinMovepath(Owner:Integer;FromFile, Tofile:string;ReNameOnCollision, Confirm:Boolean):Boolean;
//用於將目錄進行移動
var
  Struct : TSHFileOpStructA;
  MultDest: Boolean;
begin
  FillChar(Struct, SizeOf(Struct), 0);
  MultDest := pos(';', ToFile)>0;
  While pos(';', FromFile)>0 do
  FromFile[pos(';', FromFile)] := #0;
  While pos(';', ToFile)>0 do
  ToFile[pos(';', ToFile)] := #0;
  FromFile := FromFile + #0#0;
  ToFile := ToFile + #0#0;
  with Struct do
  begin
  wnd := Owner;
  wFunc := FO_Move;
  pFrom := PChar(FromFile);
  pTo := PChar(ToFile);
  fFlags := FOF_ALLOWUNDO;
  If MultDest then fFLags := fFlags or FOF_MULTIDESTFILES;
  If ReNameOnCollision then fFLags := fFlags or FOF_RENameONCOLLISION;
  If Confirm then fFLags := fFlags or FOF_NOCONFIRMATION;
  hNameMappings := nil;
  lpszProgressTitle := nil;
  end;
  result := (SHFileOperationA(Struct)=0) and (not Struct.fAnyOperationsAborted);
end;

class function TFileOper.WinMovefile(Owner:Integer;FromFile, Tofile:string;ReNameOnCollision, Confirm:Boolean):Boolean;
//用於將文件進行移動www.2cto.com
var
Struct : TSHFileOpStructA;
MultDest: Boolean;
begin
  FillChar(Struct, SizeOf(Struct), 0);
  MultDest := pos(';', ToFile)>0;
  While pos(';', FromFile)>0 do
  FromFile[pos(';', FromFile)] := #0;
  While pos(';', ToFile)>0 do
  ToFile[pos(';', ToFile)] := #0;
  FromFile := FromFile + #0#0;
  ToFile := ToFile + #0#0;
  with Struct do
  begin
  wnd := Owner;
  wFunc := FO_Move;
  pFrom := PChar(FromFile);
  pTo := PChar(ToFile);
  fFlags := FOF_ALLOWUNDO or FOF_FILESONLY;
  If MultDest then fFLags := fFlags or FOF_MULTIDESTFILES;
  If ReNameOnCollision then fFLags := fFlags or FOF_RENameONCOLLISION;
  If Confirm then fFLags := fFlags or FOF_NOCONFIRMATION;
  hNameMappings := nil;
  lpszProgressTitle := nil;
  end;
  result := (SHFileOperationA(Struct)=0) and (not Struct.fAnyOperationsAborted);
end;

class function TFileOper.WinCopypath(Owner: Integer; FromFile, Tofile: string;ReNameOnCollision, Confirm: Boolean): Boolean;
//拷貝目錄 www.2cto.com
var
  Struct : TSHFileOpStructA;
  MultDest: Boolean;
begin
  FillChar(Struct, SizeOf(Struct), 0); MultDest := pos(';', ToFile)>0;
  While pos(';', FromFile)>0 do
  FromFile[pos(';', FromFile)] := #0;
  While pos(';', ToFile)>0 do
  ToFile[pos(';', ToFile)] := #0;
  FromFile := FromFile + #0#0;
  ToFile := ToFile + #0#0;
  with Struct do
  begin
  wnd := Owner;
  wFunc := FO_Copy;
  pFrom := PChar(FromFile);
  pTo := PChar(ToFile);
  fFlags := FOF_ALLOWUNDO;
  If MultDest then
  fFLags := fFlags or FOF_MULTIDESTFILES;
  If ReNameOnCollision then
  fFLags := fFlags or FOF_RENameONCOLLISION;
  If not Confirm then
  begin
  fFLags := fFlags or FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
  end;
  hNameMappings := nil;
  lpszProgressTitle := nil;
  end;
  result := (SHFileOperationA(Struct)=0) and (not Struct.fAnyOperationsAborted);
end;

class function TFileOper.WinCopyfile(Owner: Integer; FromFile, Tofile: string;ReNameOnCollision, Confirm: Boolean): Boolean;
//拷貝文件
var
  Struct : TSHFileOpStructA;
  MultDest: Boolean;
begin
  FillChar(Struct, SizeOf(Struct), 0); MultDest := pos(';', ToFile)>0;
  While pos(';', FromFile)>0 do
    FromFile[pos(';', FromFile)] := #0;
  While pos(';', ToFile)>0 do
    ToFile[pos(';', ToFile)] := #0;
  FromFile := FromFile + #0#0;
  ToFile := ToFile + #0#0;
  with Struct do
  begin
    wnd := Owner;
    wFunc := FO_Copy;
    pFrom := PChar(FromFile);
    pTo := PChar(ToFile);
    fFlags := FOF_ALLOWUNDO or FOF_FILESONLY;
    If MultDest then
    fFLags := fFlags or FOF_MULTIDESTFILES;
    If ReNameOnCollision then
    fFLags := fFlags or FOF_RENameONCOLLISION;
    If not Confirm then
    begin
    fFLags := fFlags or FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
    end;
    hNameMappings := nil;
    lpszProgressTitle := nil;
  end;
  result := (SHFileOperationA(Struct)=0) and (not Struct.fAnyOperationsAborted);
end;

end.

摘自 #Define

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved