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 ?