0

I'm trying to make my own Mini File Manager in Windows Forms for learning purposes.

Can anyone please tell me how to implement the cancellation of the process of deleting folders, files from a PC with restoring deleted information in C#? (the progress bar is running and when you press the cancel button)

private void DeleteBtn_Click(object sender, EventArgs e)
{
        DirectoryInfo[] checkeDirectoryInfos = NodesManager.GetCheckedNodes(SourceFilesTree);

        if (MessageBox.Show("Are you sure you want permanently delete this items?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
        {
            FileManager.DeleteMethod(checkeDirectoryInfos);

            foreach (TreeNode item in NodesManager.GetCheckedTreeNodes(SourceFilesTree))
            {
                item.Remove();
            }
        }
}

// this is from NodesManager class.
private static IEnumerable<TreeNode> CheckedNodes(TreeNodeCollection nodes)
{
        foreach (TreeNode node in nodes)
        {
            // Yield this node.
            if (node.Checked)
            {
                if (!node.Parent.Checked) //if the parent is checked we don't return the children (because they are selected by default)
                {
                    yield return node;
                }
            }

            // Yield the checked descendants of this node.
            foreach (TreeNode checkedChild in CheckedNodes(node.Nodes))
                yield return checkedChild;
        }
 }

 // Return a list of the checked TreeView nodes.
 private static IEnumerable<TreeNode> CheckedNodes(TreeView trv)
 {
     return CheckedNodes(trv.Nodes);
 }

 public static DirectoryInfo[] GetCheckedNodes(TreeView tree)
 {
     return CheckedNodes(tree)
               .Select(node =>
                {
                     DirectoryInfo file = new DirectoryInfo(GetParentString(node));
                     return file;
                }).ToArray();
 }

 public static string GetParentString(TreeNode node)
 {
        if (node.Parent == null)
        {
            return node.Text;
        }

        return GetParentString(node.Parent) + node.Text + (node.Nodes.Count > 0 ? @"\" : string.Empty);
}

Delete method

public static void DeleteMethod(DirectoryInfo[] directories)
{
        foreach (DirectoryInfo dir in directories)
        {
            if ((dir.Attributes & FileAttributes.Directory) != 0) //check if its a folder
            {
                try
                {
                    dir.Delete(true);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            else // if it is a file
            {
                FileInfo file = new FileInfo(dir.FullName);

                try
                {
                    file.Delete();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }

        MessageBox.Show("Delete complete", "MiniFileManager",MessageBoxButtons.OK, MessageBoxIcon.Information);
}

This all is without a ProgressBar window form, but I want to know the idea how to implement this and how should this work(using TPL maybe with cancellation token?)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You can try cancellation token. Do your functionalities within a task, and pass a Cancellation token to the tasks, then you can cancel those from task from outside. You can have a look in this documentation https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-cancel-a-task-and-its-children – Sarker Nov 26 '18 at 09:33
  • Thank you very much for the tips, I will try – Andrei Kazheunikau Nov 26 '18 at 09:39
  • 1
    Look into file system transactions. You can rollback the operation if it's cancelled. Win32 supports it but you need pinvoke. Otherwise you can use a library like AlphaFS which has it built in – pinkfloydx33 Nov 26 '18 at 09:39

1 Answers1

0

Regarding the cancelation, it is usually done with cancelation tokens in C#.

Regarding the restoration, there is no built-in solution in .NET. You would have to move a file to your custom folder that would act as recycle bin instead of deleting them if you want to restore them. You may want to use the Shell Win32 API where you can delete files to Recycle bin and restore them. But you'll need to use P/Invoke to access Win32 API and it gets pretty complicated which doesn't seem to be what you want to do. Also you may want to consider UWP, as C# supports deleting to the Recycle bin there, but currently there is no way to restore it.

Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57