This is how i'm checking and creating a directory if the directory is not exist:
private void MakeDir(string dirName)
{
FtpWebResponse response = null;
Stream ftpStream = null;
try
{
FtpWebRequest reqFTP;
string[] Files = GetFileList();
ArrayList arrDirectories = new ArrayList();
if (Files != null)
{
foreach (string dir in Files)
{
arrDirectories.Add(dir);
}
}
if (!arrDirectories.Contains(dirName))
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + f.Host + "/" + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Proxy = null;
reqFTP.Credentials = new NetworkCredential(f.Username, f.Password);
response = (FtpWebResponse)reqFTP.GetResponse();
ftpStream = response.GetResponseStream();
}
}
catch (Exception ex)
{
if (ftpStream != null)
{
ftpStream.Close();
}
if (response != null)
{
response.Close();
}
}
}
And this is the method GetFileList:
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + f.Host + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(f.Username, f.Password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
downloadFiles = null;
return downloadFiles;
}
}
In MakeDir i give a directory name for example the name is: D
and if the directory name D not exist it will create the directory D. Untill here there are no problems. But if the directory i give is D/Test1/Test2/Test3/Test4/Test5 and let's say that only D is exist then now i need to add somehow a recursive loop to the
And another situation might be that i have already a directory strcture: D/Test1/Test2/Test3 and i give a directory to create D/Test1/Test2/Test3/Test4/Test5 in this i know that D/Test1/Test2/Test3 already exist so i need to create only: D/Test1/Test2/Test3/Test4 and then D/Test1/Test2/Test3/Test4/Test5
But in all cases it should create it like this: D/Test1/Test2/Test3 will create Test3 under D/Test1/Test2 and D/Test1/Test2/Test3/Test4 will be under D/Test1/Test2/Test3.
How can i build the recursive loop in the MadeDir method ?
In the end the idea the goal is to create the directory structure recursively.