public static void MoveDirectoryToRecycleBin( string directoryPath) { using (var fo = new FileOperation(new FileOperationProgressSink())) { fo.SetOperationFlags(FileOperationDeleteFlags); fo.DeleteItem(ZlpPathHelper.GetFullPath(directoryPath)); fo.PerformOperations(); } }
public static void DeleteFiles(IEnumerable <String> files) { Task.Run(() => { using (FileOperation fileOperation = new FileOperation()) { foreach (string file in files) { fileOperation.DeleteItem(file); } fileOperation.PerformOperations(); } }); }
protected override void OnOperate(ShellOperationEventArgs e) { // some advanced errors number can be found in %ProgramFiles(x86)%\Windows Kits\10\Include\<SDK Version>\um\sherrors.h (for example with SDK Version = 10.0.19041.0) var item = (e.Item as IObjectWithApiItem)?.ApiItem; switch (e.Operation) { case ShellOperation.RecycleItem: // our recycle bin is only local (a server side recycle bin may be better, but not implemented here) if (e.Item is IObjectWithApiItem si && e.Item.FileSystemPath != null) { // we copy the file in an accessible folder so the user will be able to get to it when pulling out from recycle bin var recycledPath = si.ApiItem.Recycle(); using (var fo = new FileOperation()) { fo.SetOperationFlags(FOF.FOFX_RECYCLEONDELETE); fo.DeleteItem(recycledPath); fo.PerformOperations(); } if (item.DeleteAsync(e.Item.FileSystemPath, new DeleteOptions { Recursive = true }).Result) { // tell the Shell not to process children (in case of a folder), as the server did it (or failed) const int COPYENGINE_S_DONT_PROCESS_CHILDREN = 0x00270008; e.HResult = COPYENGINE_S_DONT_PROCESS_CHILDREN; NotifyUpdate(); } else { // we can only reuse well-known error, for example e.HResult = ShellUtilities.ERROR_RETRY; } } break; case ShellOperation.RemoveItem: if (item.DeleteAsync(e.Item.FileSystemPath, new DeleteOptions { Recursive = true }).Result) { // tell the Shell not to process children (in case of a folder), as the server did it (or failed) const int COPYENGINE_S_DONT_PROCESS_CHILDREN = 0x00270008; e.HResult = COPYENGINE_S_DONT_PROCESS_CHILDREN; NotifyUpdate(); } else { // we can only reuse well-known error, for example e.HResult = ShellUtilities.ERROR_RETRY; } break; case ShellOperation.RenameItem: case ShellOperation.SetNameOf: var result = item.RenameAsync(e.NewName, e.Item.FileSystemPath).Result; if (result != null) { e.HResult = ShellUtilities.S_OK; NotifyUpdate(); } else { // we can only reuse well-known error, for example e.HResult = ShellUtilities.ERROR_RETRY; } break; } base.OnOperate(e); }