this slowpoke moves

Delete locked Files

function DeleteFileDuringNextSystemBoot(aFileName: string): Boolean;
var
  Dummy, ShortFileName: array [0..MAX_PATH + 1] of Char;
  WinIni: string;
  SystemType: TOSVersionInfoA;
begin
  Result := False;

  SystemType.dwOSVersionInfoSize := SizeOf(SystemType);
  GetVersionEx(SystemType);

  if SystemType.dwPlatformId = VER_PLATFORM_WIN32s then
    // nicht unterstützt durch Win32s Systeme :(
    Exit;

  if SystemType.dwPlatformId = VER_PLATFORM_WIN32_NT then
  begin
    if StrLen(PChar(ExtractFileDrive(aFileName))) < 4 then
      Result := False // Falls Filename z. B. nur aus C:\ besteht
    else
      Result := MoveFileEx(PChar(aFileName), nil, MOVEFILE_REPLACE_EXISTING +
        MOVEFILE_DELAY_UNTIL_REBOOT);
  end else begin
    GetWindowsDirectory(Dummy, MAX_PATH + 1);
    WinIni := Dummy;
    if AnsiLastChar(WinIni)^ <> '\' then
      WinIni := WinIni + '\wininit.ini'
    else
      WinIni := WinIni + 'wininit.ini';

    with TStringList.Create do
    try
      if FileExists(WinIni) then
        LoadFromFile(WinIni);
      if IndexOf('[rename]') < 0 then
        Add('[rename]');
      GetShortPathName(PChar(aFileName), ShortFileName, MAX_PATH + 1);
      Insert(Succ(IndexOf('[rename]')), 'NUL=' + ShortFileName);

      SaveToFile(WinIni);
      Result := True;
    finally
      Free;
    end;
  end;
end;
Beispiel :
procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteFileDuringNextSystemBoot('C:\temp.tmp')
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate