如何强制删除正在使用的文件

来源:互联网 发布:手机淘宝怎么定位地址 编辑:程序博客网 时间:2024/04/29 05:07

如何强制删除正在使用的文件
好象以前有一个函数可以删除程序本身,找不到了,估计可以参考一下,请各位大侠指教
---------------------------------------------------------------

procedure DeleteSelf;
var
  pExitProcess: Pointer;
  pDeleteFile: Pointer;
  pUnmapViewOfFile: Pointer;
  hModule: THANDLE;
  cBuf: array[0..MAX_PATH] of Char;
begin
  hModule := GetModuleHandle('kernel32');
  if hModule <> 0 then
  begin
    pExitProcess := GetProcAddress(hModule, 'ExitProcess');
    pDeleteFile := GetProcAddress(hModule, 'DeleteFileA');
    pUnmapViewOfFile := GetProcAddress(hModule, 'UnmapViewOfFile');
  end else
  begin
    pExitProcess := nil;
    pDeleteFile := nil;
    pUnmapViewOfFile := nil;
  end;
  hModule := GetModuleHandle(nil);
  GetModuleFileName(hModule, cBuf, Sizeof(cBuf));
  CloseHandle(THANDLE(4));
  asm
    XOR EAX, EAX;
    PUSH EAX;
    PUSH EAX;
    LEA EAX, cBuf;
    PUSH EAX;
    MOV EAX, pExitProcess;
    PUSH EAX;
    MOV EAX, hModule;
    PUSH EAX;
    MOV EAX, pDeleteFile;
    PUSH EAX;
    MOV EAX, pUnmapViewOfFile;
    PUSH EAX;
    RET;
  end;
end;
end.

---------------------------------------------------------------

如何强制删除正在使用的文件
好象以前有一个函数可以删除程序本身,找不到了,估计可以参考一下,请各位大侠指教
--------------------
正在運行的應用程序和正在使用的文件是不同的概念.
---------------------------------------------------------------

如何在NT/2000中删除一个正在使用的文件

<> MoveFileEx <> 
这个函数允许我们对一个指定的文件或目录改名。但是如果我们在dwFlags中指定为TMOVEFILE_DELAY_UNTIL_REBOOT(仅对NT/2000有效),那么这次更动会在重启之后生效,系统会在重启之后删除或改改我们指定的文件。

用法:
MoveFileEx(ExistingFN, NewFN, MOVEFILE_REPLACE_EXISTING)
MoveFileEx(ExistingFN, NewFN, MOVEFILE_DELAY_UNTIL_REBOOT)

其中:
ExistingFN 为指定的文件或目录的路径。
NewFN 为新的文件或目录名。

标志 MOVEFILE_REPLACE_EXISTING 告诉函数如果文件或目录存在则改写它的名称。
如果 NewFN 设为 nil,那么则将其删除

例子:

移动文件:
MoveFileEx('c:/winnt/system32/kernel32.dll', 'd:/winnt.bak/system32/kernel32.dll',MOVEFILE_REPLACE_EXISTING);
MoveFileEx('c:/winnt/system32/kernel32.dll', 'd:/winnt.bak/system32/kernel32.dll',MOVEFILE_DELAY_UNTIL_REBOOT); 

删除文件:
MoveFileEx('c:winnt/system32/kernel32.dll', nil,MOVEFILE_REPLACE_EXISTING);
MoveFileEx('c:winnt/system32/kernel32.dll', nil,MOVEFILE_DELAY_UNTIL_REBOOT);


 强制的,我要想想
---------------------------------------------------------------

这个是深入windows核心编程里提供的代码(删除或移动正在使用的文件)如下:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;
 
const
   FILE_DELETE=1;
   FILE_RENAME=2;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    RadioGroup1: TRadioGroup;
    Edit1: TEdit;
    Edit2: TEdit;
    Button2: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
var
  WindowsDirs: array [0..MAX_PATH + 1] of Char;
  lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of Char;
  VerPlatForm: TOSVersionInfoA;
  StrLstDelte: TStrings;
  filename,s  :String;
  i:integer;
begin
  Result := FALSE;
  ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
  VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
  GetVersionEx(VerPlatForm);
  if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
  begin
     SetLastError(ERROR_NOT_SUPPORTED);
     Exit;
  end
  else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
  begin
     if flag=FILE_DELETE then
        Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
          MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
     else if (flag=FILE_RENAME) then
        Result := MoveFileEx(lpFileNameToSrc,