分享

Delphi删除或移动正在使用的文件

 quasiceo 2016-12-18

Delphi删除或移动正在使用的文件

  • 标签:删除文件 更新时间:2013-12-02

  • Delphi删除文件容易,但删除正在使用的文件,那就需要手段了,因为正在使用的文件是不允许被删除的,所以要想知道如何实现,或许你会从下面的代码中得到启发,其实很简单,呵呵,不说了,看代码:

    001unit Unit1;
    002interface
    003uses
    004  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    005  StdCtrls, ExtCtrls;
    006const
    007   FILE_DELETE=1;
    008   FILE_RENAME=2;
    009type
    010  TForm1 = class(TForm)
    011    Button1: TButton;
    012    Label1: TLabel;
    013    Label2: TLabel;
    014    RadioGroup1: TRadioGroup;
    015    Edit1: TEdit;
    016    Edit2: TEdit;
    017    Button2: TButton;
    018    Button3: TButton;
    019    OpenDialog1: TOpenDialog;
    020    procedure Button2Click(Sender: TObject);
    021    procedure Button3Click(Sender: TObject);
    022    procedure Button1Click(Sender: TObject);
    023    procedure Edit2Change(Sender: TObject);
    024    procedure RadioGroup1Click(Sender: TObject);
    025  private
    026    { Private declarations }
    027  public
    028    { Public declarations }
    029  end;
    030var
    031  Form1: TForm1;
    032implementation
    033{$R *.DFM}
    034function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
    035var
    036  WindowsDirs: array [0..MAX_PATH + 1] of Char;
    037  lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of Char;
    038  VerPlatForm: TOSVersionInfoA;
    039  StrLstDelte: TStrings;
    040  filename,s  :String;
    041  i:integer;
    042begin
    043  Result := FALSE;
    044  ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
    045  VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
    046  GetVersionEx(VerPlatForm);
    047  if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
    048  begin
    049     SetLastError(ERROR_NOT_SUPPORTED);
    050     Exit;
    051  end
    052  else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
    053  begin
    054     if flag=FILE_DELETE then
    055        Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
    056          MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
    057     else if (flag=FILE_RENAME) then
    058        Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes,
    059          MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
    060  end
    061  else begin
    062     StrLstDelte := TStringList.Create;
    063     GetWindowsDirectory(WindowsDirs, MAX_PATH + 1);
    064     filename:=WindowsDirs;
    065     if filename[length(filename)]<>'\' then filename:=filename+'\';
    066     filename:=filename+'wininit.ini';
    067     if FileExists(filename) then
    068        StrLstDelte.LoadFromFile(filename);
    069     if StrLstDelte.IndexOf('[rename]') = -1 then
    070        StrLstDelte.Add('[rename]');
    071     GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + 1);
    072     if fileexists(lpFileNameToDes) then
    073        GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + 1)
    074     else begin
    075        s:=extractfilename(lpFileNameToDes);
    076        i:=pos('.',s);
    077        if (i=0) then
    078        begin
    079           if length(s)>8 then raise exception.create('不是有效的短文件名(8+3格式)!');
    080        end
    081        else begin
    082           if (i-1>8)or(length(s)-i>3) then raise exception.create('不是有效的短文件名(8+3格式)!');
    083        end;
    084        strcopy(lpDirDes,lpFileNameToDes);
    085     end;
    086     if (flag=FILE_DELETE) then {删除}
    087        StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, 'NUL='+string(lpDirSrc))
    088     else if (flag=FILE_RENAME) then {改名}
    089        StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, string(lpDirDes)+'='+string(lpDirSrc));
    090 
    091     StrLstDelte.SaveToFile(filename);
    092     Result := TRUE;
    093     StrLstDelte.Free;
    094  end;
    095end;
    096procedure TForm1.Button2Click(Sender: TObject);
    097begin
    098   if OpenDialog1.Execute then
    099      edit1.text:=OpenDialog1.FileName;
    100end;
    101 
    102procedure TForm1.Button3Click(Sender: TObject);
    103begin
    104   if OpenDialog1.Execute then
    105      edit2.text:=OpenDialog1.FileName;
    106end;
    107procedure TForm1.Button1Click(Sender: TObject);
    108var
    109   i:uint;
    110begin
    111   if RadioGroup1.ItemIndex=0 then i:=FILE_DELETE
    112   else i:=FILE_RENAME;
    113   if edit1.text='' then raise exception.create('源文件为空!');
    114   if (i=FILE_RENAME)and(edit2.text='') then raise exception.create('目标文件为空!');
    115   if not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
    116      showmessage('出错了')
    117   else showmessage('操作完成');
    118end;
    119 
    120procedure TForm1.Edit2Change(Sender: TObject);
    121var
    122  VerPlatForm: TOSVersionInfoA;
    123  buf: array [0..MAX_PATH + 1] of Char;
    124begin
    125  if not fileexists(edit2.text) then exit;
    126  ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
    127  VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
    128  GetVersionEx(VerPlatForm);
    129  if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
    130  begin
    131     GetShortPathName(pchar(edit2.text), buf, MAX_PATH + 1);
    132     edit2.text:=buf;
    133  end;
    134end;
    135procedure TForm1.RadioGroup1Click(Sender: TObject);
    136begin
    137   edit2.Enabled:=RadioGroup1.ItemIndex=1;
    138   button2.Enabled:=RadioGroup1.ItemIndex=1;
    139end;
    140end.

    其实就是利用Windows重启的瞬间来删除或移动文件。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多