How to “safely” delete folder into Recycle Bin

后端 未结 2 532
自闭症患者
自闭症患者 2020-12-10 14:34

I\'m looking for a way to put a folder (with subfolders) into a Recycle Bin with these conditions:

  1. It must be done silently -- without any

相关标签:
2条回答
  • 2020-12-10 15:27

    I was able to come up with a solution to all 3 of my original points/requests.

    In a nutshell, one needs to use the IFileOperation interface and implement IFileOperationProgressSink in it.

    Here's full code sample and explanation for that.

    EDIT: Ok, there's more to it. The method I posted above doesn't cover all bases :(

    0 讨论(0)
  • 2020-12-10 15:29

    Every drive has its own Recycle Bin. And when you delete file from drive С: it should be moved to Recycle Bin on drive С:. When you delete file from USB drive it should be moved to Recycle Bin on USB drive. But when USB drive has no Recycle Bin then file is permanently deleted. This is default Windows behavior.

    FOF_ALLOWUNDO flag is RECOMMENDATION only. MSDN says about FOF_ALLOWUNDO flag:

    Preserve undo information, if possible.

    So there is no any error when Windows permanently deleted files even when you use FOF_ALLOWUNDO flag.

    The only way I see is to check presence of Recycle Bin on drive with SHQueryRecycleBin function (as pointed by Alex Farber in comment) before delete operation. But even if Recycle Bin presents it is not full guaranty that file will be deleted to Recycle Bin. Recycle Bin has maximal limit of size and it can be already full.

    UPDATE

    You can use hack. You can emulate removing of file into Recycle Bin with you own code which will create all necessary system records in C:\$Recycle.Bin\UserSID folder. I tested this method on Windows 7 and it works correctly. It allows to ignore limitation of max size of Recycle Bin. Also it allows to move files from USB into Recycle Bin on any drive.

    UPDATE 2

    For Vista+ you can use undocumented interface IRecycleBinManager (Russian description can be found on webpage http://rcrrad.com/2010/10/14/bitbucket-interfaces/):

    const
      IID_IEnumRecycleItems: TGUID = '{6E325F88-D12F-49E5-895B-8EC98630C021}';
      IID_IRecycle: TGUID = '{0125E62F-8349-443A-854B-A55FB84CFA35}';
      IID_IRecycleBin: TGUID = '{F964AD97-96F4-48AB-B444-E8588BC7C7B3}';
      IID_IRecycleBinManager: TGUID = '{5869092D-8AF9-4A6C-AE84-1F03BE2246CC}';
      CLSID_RecycleBinManager: TGUID = '{4A04656D-52AA-49DE-8A09-CB178760E748}';
    
    type
      { Тип Корзины }
      tagRECYCLEBIN_TYPE = (RBTYPE_VOLUME, RBTYPE_KNOWNFOLDER);
      TRecycleBinType = tagRECYCLEBIN_TYPE;
    
      { Данные об удаленном элементе }
      PDeletedItem = ^TDeletedItem;
      tagDELETEDITEM = packed record
        dwFileSizeLow: DWORD;
        dwFileSizeHigh: DWORD;
        ftDeletionTime: TFileTime;
        szOriginalPath: array[0..Pred(MAX_PATH)] of WideChar;
        szDisplacedPath: array[0..Pred(MAX_PATH)] of WideChar;
      end;
      TDeletedItem = tagDELETEDITEM;
    
      { Перечислитель элементов Корзины }
      IEnumRecycleItems = interface(IUnknown)
        ['{6E325F88-D12F-49E5-895B-8EC98630C021}']
        { celt может быть равен только единице }
        function Next(celt: ULONG; out rgelt: TDeletedItem;
          var pceltFetched: ULONG): HRESULT; stdcall;
        { Not Implemented }
        function Skip(celt: ULONG): HRESULT; stdcall;
        function Reset: HRESULT; stdcall;
        { Not Implemented }
        function Clone(out ppenum: IEnumRecycleItems): HRESULT; stdcall;
      end;
    
      { "Интерфейс-переходник" между IRecycleBin и IRecycleBinManager }
      IRecycle = interface(IUnknown)
        ['{0125E62F-8349-443A-854B-A55FB84CFA35}']
        function Compact(): HRESULT; stdcall;
        function GetFileData(const pszPath: LPCWSTR;
          out lpData: TDeletedItem): HRESULT; stdcall;
        function GetItemCount(out lpCount: TLargeInteger): HRESULT; stdcall;
        function GetUsedSpace(out lpUsedSpace: TLargeInteger): HRESULT; stdcall;
        function IsEmpty(): HRESULT; stdcall;
        function PurgeAll(pfo: IFileOperation): HRESULT; stdcall;
        function PurgeItems(const lpstrItems: LPCWSTR;
          pfo: IFileOperation): HRESULT; stdcall;
        function SuspendUpdating(fSuspend: BOOL): HRESULT; stdcall;
        function RecycleItem(const lpstrItem: LPCWSTR; const dwAttrs: DWORD;
          const iFileSize: TLargeInteger; out psi: IShellItem): HRESULT; stdcall;
        function RestoreItems(const lpstrItems: LPCWSTR;
          pfo: IFileOperation): HRESULT; stdcall;
        function IsRecycled(const pszPath: LPCWSTR;
          lpRecycled: PBOOL): HRESULT; stdcall;
        function EnumItems(dwFlags: DWORD;
          out EnumRecycleItems: IEnumRecycleItems): HRESULT; stdcall;
        function WillRecycle(const pszPath: LPCWSTR): HRESULT; stdcall;
      end;
    
      { Представляет определенную Корзину на конкретном диске }
      IRecycleBin = interface(IUnknown)
        ['{F964AD97-96F4-48AB-B444-E8588BC7C7B3}']
        function Compact(): HRESULT; stdcall;
        function GetFileData(const pszPath: LPCWSTR;
          out lpData: TDeletedItem): HRESULT; stdcall;
        function GetItemCount(out lpCount: TLargeInteger): HRESULT; stdcall;
        function GetUsedSpace(out lpUsedSpace: TLargeInteger): HRESULT; stdcall;
        function IsEmpty(): HRESULT; stdcall;
        function PurgeAll(pfo: IFileOperation): HRESULT; stdcall;
        function PurgeItems(const lpstrItems: LPCWSTR;
          pfo: IFileOperation): HRESULT; stdcall;
        function SuspendUpdating(fSuspend: BOOL): HRESULT; stdcall;
        function RecycleItem(const lpstrItem: LPCWSTR; const dwAttrs: DWORD;
          const iFileSize: TLargeInteger; out psi: IShellItem): HRESULT; stdcall;
        function RestoreItems(const lpstrItems: LPCWSTR;
          pfo: IFileOperation): HRESULT; stdcall;
        function IsRecycled(const pszPath: LPCWSTR;
          lpRecycled: PBOOL): HRESULT; stdcall;
        function EnumItems(dwFlags: DWORD;
          out EnumRecycleItems: IEnumRecycleItems): HRESULT; stdcall;
        function WillRecycle(const pszPath: LPCWSTR): HRESULT; stdcall;
        function Initialize(const rbType: TRecycleBinType;
          const pszID: LPCWSTR): HRESULT; stdcall;
        function GetTypeID(out rbType: TRecycleBinType;
          var pszID: LPWSTR): HRESULT; stdcall;
        function GetIDList(out ppidl: PItemIDList): HRESULT; stdcall;
        function GetLocation(pszPathBuffer: LPWSTR;
          cchMax: DWORD): HRESULT; stdcall;
        function GetMaxCapacityRange(out lpMin: TLargeInteger;
          out lpMax: TLargeInteger): HRESULT; stdcall;
        function GetMaxCapacity(out lpCapacity: TLargeInteger): HRESULT; stdcall;
        function SetMaxCapacity(const lpCapacity: TLargeInteger): HRESULT; stdcall;
        function GetPurgeOnDelete(out fNukeOnDelete: BOOL): HRESULT; stdcall;
        function SetPurgeOnDelete(const fNukeOnDelete: BOOL): HRESULT; stdcall;
      end;
    
      { Менеджер всех Корзин данной ОС }
      IRecycleBinManager = interface(IUnknown)
        ['{5869092D-8AF9-4A6C-AE84-1F03BE2246CC}']
        function Compact(): HRESULT; stdcall;
        function GetFileData(const pszPath: LPCWSTR;
          out lpData: TDeletedItem): HRESULT; stdcall;
        function GetItemCount(out lpCount: TLargeInteger): HRESULT; stdcall;
        function GetUsedSpace(out lpUsedSpace: TLargeInteger): HRESULT; stdcall;
        function IsEmpty(): HRESULT; stdcall;
        function PurgeAll(pfo: IFileOperation): HRESULT; stdcall;
        function PurgeItems(const lpstrItems: LPCWSTR;
          pfo: IFileOperation): HRESULT; stdcall;
        function SuspendUpdating(fSuspend: BOOL): HRESULT; stdcall;
        { Not Implemented }
        function RecycleItem(const lpstrItem: LPCWSTR; const dwAttrs: DWORD;
          const iFileSize: TLargeInteger; out psi: IShellItem): HRESULT; stdcall;
        function RestoreItems(const lpstrItems: LPCWSTR;
          pfo: IFileOperation): HRESULT; stdcall;
        function IsRecycled(const pszPath: LPCWSTR;
          lpRecycled: PBOOL): HRESULT; stdcall;
        function EnumItems(dwFlags: DWORD;
          out EnumRecycleItems: IEnumRecycleItems): HRESULT; stdcall;
        function WillRecycle(const pszPath: LPCWSTR): HRESULT; stdcall;
        function DelayCompaction(const fDelay: BOOL): HRESULT; stdcall;
        function GetRecycleBinCount(out iCount: Integer): HRESULT; stdcall;
        function GetRecycleBinAt(const index: Integer; const iid: TGUID;
          out ppv): HRESULT; stdcall;
        function GetRecycleBin(const pszPath: LPCWSTR; const iid: TGUID;
          out ppv): HRESULT; stdcall;
        function Refresh(): HRESULT; stdcall;
      end;
    

    You can check the possibility of deletion of the file into Recycle Bin with the following code:

    function CanFileBeDeletedToRecycleBin(const AFileName: UnicodeString): Boolean;
    var
      RecycleBinManager: IRecycleBinManager;
    begin
      OleCheck(CoCreateInstance(CLSID_RecycleBinManager, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IRecycleBinManager, RecycleBinManager));
      try
        Result := RecycleBinManager.WillRecycle(PWideChar(AFileName)) = S_OK;
      finally
        RecycleBinManager := nil;
      end;
    end;
    

    UPDATE 3

    Also you can try the following code for delete oject into Recycle Bin:

    function GetObjectSize(const AFileName: UnicodeString): Int64;
    var
      FindHandle: THandle;
      FindData: TWin32FindDataW;
      S: Int64;
    begin
      Result := 0;
      FindHandle := FindFirstFileW(PWideChar(AFileName), FindData);
      if FindHandle = INVALID_HANDLE_VALUE then
        RaiseLastOSError;
      try
        repeat
          if (FindData.cFileName <> UnicodeString('.')) and (FindData.cFileName <> '..') then
            begin
              Int64Rec(S).Lo := FindData.nFileSizeLow;
              Int64Rec(S).Hi := FindData.nFileSizeHigh;
              Result := Result + S;
              if FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
                Result := Result + GetObjectSize(AFileName + '\*.*');
            end;
        until not FindNextFileW(FindHandle, FindData);
      finally
        FindClose(FindHandle);
      end;
    end;
    
    procedure DeleteToRecycleBin(const AFileName: UnicodeString);
    var
      Attr: DWORD;
      Size: Int64;
      RecycleBinManager: IRecycleBinManager;
      RecycleBin: IRecycleBin;
      ShellItem: IShellItem;
    begin
      OleCheck(CoCreateInstance(CLSID_RecycleBinManager, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IRecycleBinManager, RecycleBinManager));
      try
        OleCheck(RecycleBinManager.GetRecycleBin(PWideChar(AFileName), IRecycleBin, RecycleBin));
        try
          Attr := GetFileAttributes(PWideChar(AFileName));
          Size := GetObjectSize(AFileName);
          OleCheck(RecycleBin.RecycleItem(PWideChar(AFileName), Attr, Size, ShellItem));
          ShellItem := nil;
        finally
          RecycleBin := nil;
        end;
      finally
        RecycleBinManager := nil;
      end;
    end;
    
    0 讨论(0)
提交回复
热议问题