Example #1
0
 public static void MoveDirectoryToRecycleBin(
     string directoryPath)
 {
     using (var fo = new FileOperation(new FileOperationProgressSink()))
     {
         fo.SetOperationFlags(FileOperationDeleteFlags);
         fo.DeleteItem(ZlpPathHelper.GetFullPath(directoryPath));
         fo.PerformOperations();
     }
 }
Example #2
0
 public static void MoveDirectoryToRecycleBin(
     string directoryPath)
 {
     using (var fo = new FileOperation(new FileOperationProgressSink()))
     {
         fo.SetOperationFlags(FileOperationDeleteFlags);
         fo.DeleteItem(ZlpPathHelper.GetFullPath(directoryPath));
         fo.PerformOperations();
     }
 }
        protected override void OnDragDropTarget(DragDropTargetEventArgs e)
        {
            e.HResult = ShellUtilities.S_OK;
            var paths = GetPaths(e);

            if (paths.Count > 0)
            {
                e.Effect = DragDropEffects.All;
            }

            if (e.Type == DragDropTargetEventType.DragDrop)
            {
                // file operation events need an STA thread
                WindowsUtilities.DoModelessAsync(() =>
                {
                    using (var fo = new FileOperation(true))
                    {
                        fo.PostCopyItem += (sender, e2) =>
                        {
                            // we could add some logic here
                        };

                        if (paths.Count == 1)
                        {
                            fo.CopyItem(paths[0], FileSystemPath, null);
                        }
                        else
                        {
                            fo.CopyItems(paths, FileSystemPath);
                        }
                        fo.SetOperationFlags(FOF.FOF_ALLOWUNDO | FOF.FOF_NOCONFIRMMKDIR | FOF.FOF_RENAMEONCOLLISION);
                        fo.PerformOperations();
                        NotifyUpdate();
                    }
                });
            }
        }
Example #4
0
        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);
        }