AQC (Another Qaility Code): function ProtectFile(sFilename : string) : hFile; var hf: hFile; lwHFileSize, lwFilesize: longword; ofs : TOFStruct; begin if FileExists(sFilename) then begin hf := OpenFile(pchar(sFilename), ofs, OF_READ or OF_WRITE or OF_SHARE_EXCLUSIVE); if hf <> 0 then begin lwFilesize := GetFileSize(hf, @lwHFileSize); if LockFile(hf, 0, 0, lwFilesize, lwHFilesize) then Result := hf else Result := 0; end else Result := 0; end else Result := 0; end;function UnProtectFile(hf: hFile): boolean; begin if hf <> 0 then CloseHandle(hf); end;procedure TForm1.Button1Click(Sender: TObject); begin protectedFile := ProtectFile(ExtractFilePath(Application.Exename)+'test.txt'); if protectedFile = 0 then MessageBox(Handle, 'Error locking file!', '', MB_OK or MB_ICONERROR) else Button1.Enabled := False; end;procedure TForm1.Button2Click(Sender: TObject); var plik: TextFile; data: string; begin UnProtectFile(protectedFile); AssignFile(plik, 'test.txt'); Reset(plik); Read(plik, data); CloseFile(plik); protectedFile := ProtectFile(ExtractFilePath(Application.Exename)+'test.txt'); ShowMessage(data); end;
function ProtectFile(sFilename : string) : hFile;
var
hf: hFile;
lwHFileSize, lwFilesize: longword;
ofs : TOFStruct;
begin
if FileExists(sFilename) then
begin
hf := OpenFile(pchar(sFilename), ofs, OF_READ or OF_WRITE or OF_SHARE_EXCLUSIVE);
if hf <> 0 then
begin
lwFilesize := GetFileSize(hf, @lwHFileSize);
if LockFile(hf, 0, 0, lwFilesize, lwHFilesize) then
Result := hf else Result := 0;
end
else Result := 0;
end
else Result := 0;
end;function UnProtectFile(hf: hFile): boolean;
begin
if hf <> 0 then
CloseHandle(hf);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
protectedFile := ProtectFile(ExtractFilePath(Application.Exename)+'test.txt');
if protectedFile = 0 then
MessageBox(Handle, 'Error locking file!', '', MB_OK or MB_ICONERROR) else
Button1.Enabled := False;
end;procedure TForm1.Button2Click(Sender: TObject);
var
plik: TextFile;
data: string;
begin
UnProtectFile(protectedFile); AssignFile(plik, 'test.txt');
Reset(plik);
Read(plik, data);
CloseFile(plik); protectedFile := ProtectFile(ExtractFilePath(Application.Exename)+'test.txt');
ShowMessage(data);
end;
Windows提供ICopyHook,其中提供了一个函数:原形:function CopyCallback(Wnd: HWND; wFunc, wFlags: UINT; pszSrcFile: PAnsiChar;
dwSrcAttribs: DWORD; pszDestFile: PAnsiChar; dwDestAttribs: DWORD): UINT; stdcall;function TCopyMain.CopyCallback(Wnd: HWND; wFunc, wFlags: UINT;
pszSrcFile: PAnsiChar; dwSrcAttribs: DWORD; pszDestFile: PAnsiChar;
dwDestAttribs: DWORD): UINT;
const
FO_COPY = 2;
FO_DELETE = 3;
FO_MOVE = 1;
FO_RENAME = 4;
begin
if wFunc = FO_DELETE then
Do something...
end;做好in prodess并且注册以后,注销WINDOWS,然后重新启动,当你删除文件夹的时候就会自动进入你的程序。以上程序只能删除文件夹,对文件不管用。
文件可以另做,不过比较简单,可以用API以独占方式打开。
要删除有何难?DOS实模式下删除,一定OK!http://lysoft.7u7.net