如题,望高手赐教, 因为我在设计软件在线更新时需要删除自身或者备份自身

解决方案 »

  1.   

    先定义两个常量:
    const
       FILE_DELETE=1;
       FILE_RENAME=2;
    这个函数用来删除或移动
    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, lpFileNameToDes,
              MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
      end
      else begin
         StrLstDelte := TStringList.Create;
         GetWindowsDirectory(WindowsDirs, MAX_PATH + 1);
         filename:=WindowsDirs;
         if filename[length(filename)]<>'\' then filename:=filename+'\';
         filename:=filename+'wininit.ini';
         if FileExists(filename) then
            StrLstDelte.LoadFromFile(filename);
         if StrLstDelte.IndexOf('[rename]') = -1 then
            StrLstDelte.Add('[rename]');
         GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + 1);
         if fileexists(lpFileNameToDes) then
            GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + 1)
         else begin
            s:=extractfilename(lpFileNameToDes);
            i:=pos('.',s);
            if (i=0) then
            begin
               if length(s)>8 then raise exception.create('不是有效的短文件名(8+3格式)!');
            end
            else begin
               if (i-1>8)or(length(s)-i>3) then raise exception.create('不是有效的短文件名(8+3格式)!');
            end;
            strcopy(lpDirDes,lpFileNameToDes);
         end;
         if (flag=FILE_DELETE) then {删除}
            StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, 'NUL='+string(lpDirSrc))
         else if (flag=FILE_RENAME) then {改名}
            StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, string(lpDirDes)+'='+string(lpDirSrc));     StrLstDelte.SaveToFile(filename);
         Result := TRUE;
         StrLstDelte.Free;
      end;
    end;调用方式:
    edit1表示源文件,
    edit2表示目标文件。
    procedure TForm1.Button1Click(Sender: TObject);
    var
       i:uint;
    begin
       if RadioGroup1.ItemIndex=0 then i:=FILE_DELETE
       else i:=FILE_RENAME;
       if edit1.text='' then raise exception.create('源文件为空!');
       if (i=FILE_RENAME)and(edit2.text='') then raise exception.create('目标文件为空!');
       if not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
          showmessage('出错了')
       else showmessage('ok');
    end;
      

  2.   

    谢谢 winxkm(蹩脚的程序员) 你好厉害,以后请多多指教!