0

In this event i select the option from the Context Strip Menu in this case "Delete" And then i check if the node i selected in the treeView1 is a File or Directory.

private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            bool file = false;
            if (e.ClickedItem.Text == "Delete")
            {
                if (treeView1.SelectedNode.Tag != null)
                {
                    string s = (string)treeView1.SelectedNode.Tag; // this is casting
                    if (s == "file")
                    {
                        file = false;
                        DeleteFile(treeView1.SelectedNode.FullPath,file);
                    }
                }
                else
                {
                    file = true;
                    DeleteFile(treeView1.SelectedNode.FullPath,file);
                }
            }
        }

Then in the DeleteFile method i check if it's a file make DeleteFile if a it's a directory make RemoveDirectory.

private void DeleteFile(string remoteFile, bool fileordir)
        {
            remoteFile = remoteFile.Replace('\\', '/');
            if (remoteFile.StartsWith("root"))
            {
                remoteFile = remoteFile.Replace("root", string.Empty);
            }
            string deleteRequest = txtHost.Text + remoteFile;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + deleteRequest);
            if (fileordir == false)
            {
                request.Method = WebRequestMethods.Ftp.DeleteFile;
            }
            else
            {
                request.Method = WebRequestMethods.Ftp.RemoveDirectory;
            }
            request.Credentials = new NetworkCredential(txtUsername.Text, txtPassword.Text);
            request.Proxy = null;
            request.UseBinary = false;
            request.UsePassive = true;
            request.KeepAlive = false;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream);
            sr.ReadToEnd();
            string StatusCode = response.StatusDescription;
            sr.Close();
            response.Close();
        }

The problem is if i want to RemoveDirectory that have inside sub directories and files. For example if remoteFile contain: /b/c/d/e/test.jpg then it's file and it will delete test.jpg Also if remoteFile is: /test.jpg it's a file and it will delete test.jpg

But now i want to delete a directory i selected in the treeView a directory node but i don't know if and how many sub directories and files might be inside.

For example if in remoteFile i have: /b/c/d/e/f/g i will get error since RemoveDirectory dosen't know to delete it as one string i must make a recursive loop first to delete: /b/c/d/e/f/g this will delete the sub directory g Then /b/c/d/e/f will delete the directory f then /b/c/d/e then b/c/d then /b/c then /b

And if in those sub directories somewhere there is a file/s i need somehow also to use in the recursive loop also the FileDelete.

How can i build the recursive loop when i select a node from the treeView and it's a directory and need to make RemoveDirectory ?

Jorge Hyyest
  • 301
  • 1
  • 6
  • 16

2 Answers2

1

To recursively remove directories, your "RemoveDirectory" method should:

  • accept a path to the directory to remove
  • read all (names of) files and subdirectories inside that directory (just directly inside it, no deeper levels)
  • remove all files that you found
  • for all subdirectories: recursively call this same RemoveDirectory method, using this subdirectory as argument
  • finally remove the now empty directory
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
1

Try something like this

public static class FooClass
    {
        public void DeleteDirectory(string path)
        {
            foreach (string directory in Directory.EnumerateDirectories(path))
            {
                DeleteDirectory(directory);
            }
            foreach (string file in Directory.EnumerateFiles(path))
            {
                File.Delete(file);
            }
            Directory.Delete(path);
        }
    }

Then, you will just need to pass the starting path to the function and it will recursively delete first all the files of the furthest directory, delete it, and keep going until it gets to the specified directory level.

Beronien
  • 33
  • 6