Reputation: 67
I'm trying to download all files including sub folders from ftp in c#.
I created DownloadDiretory for recursion of the sub folders and DownloadFtpFile for downloading files.
The code works fine for the root folder file download, but it is not downloading any subfolder files.
Any suggestions?
Thanks in Advance.
public static void DownloadDiretory(string folderPath)
{
try
{
ConnectionsXml objXml = new ConnectionsXml();
AccountFtp account = objXml.GetAccountFtpDetails();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + account.Website + "/" + folderPath);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(account.UserId, account.Password);
request.Timeout = 360000;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//FtpWebResponse response = GetFtpResponse(folderPath);
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, true);
string filename;
while (!reader.EndOfStream)
{
filename = string.Empty;
filename = reader.ReadLine();
if (filename.Contains("<DIR>"))
{
filename = filename.Substring(filename.IndexOf("<DIR>", 0) + 5, filename.Length - (filename.IndexOf("<DIR>", 0) + 5));
filename = filename.Trim();
DownloadDiretory(folderPath + "/" + filename);
}
else
{
string[] files = filename.Split(' ');
filename = files[files.Length - 1];
DownloadFtpFile(folderPath, filename);
}
}
responseStream.Close();
response.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
public static void DownloadFtpFile(string folderName, string fileName)
{
try
{
ConnectionsXml objXml = new ConnectionsXml();
AccountFtp account = objXml.GetAccountFtpDetails();
string path = "ftp://" + account.Website + "/" + folderName + "/" + fileName;
FtpWebRequest request = (FtpWebRequest)WebRequest.CreateDefault(new Uri(path));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(account.UserId, account.Password);
//request.Timeout = 360000;
request.KeepAlive = false;
request.UsePassive = true;
request.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
if(!Directory.Exists(@"F:\MPR\" + folderName))
{
Directory.CreateDirectory(@"F:\MPR\" + folderName);
}
FileStream fileStream = new FileStream(@"F:\MPR\" + folderName + @"\" + fileName, FileMode.Create);
byte[] bytesbuffer = new byte[32 * 1024];
int byteRead = responseStream.Read(bytesbuffer, 0, 2048);
while (byteRead > 0)
{
fileStream.Write(bytesbuffer, 0, byteRead);
byteRead = responseStream.Read(bytesbuffer, 0, 2048);
}
responseStream.Close();
fileStream.Close();
response.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
Upvotes: 2
Views: 587
Reputation: 3580
Your condition filename.Contains("DIR"))
seems to be wrong. I have no private ftp server available, so I tried on ftp://test.talia.net. The folder "incoming" gets returned as
"drwxrwxr-x 2 ftp ftp 4096 Oct 15 07:32 incoming"
Files get returned as eg
"-rw-r--r-- 1 ftp ftp 10485760 Apr 19 2006 10mb.pak"
So try to check with filename.StartsWith("d")
.
Also, these lines:
filename = string.Empty;
filename = reader.ReadLine();
serve no real purpose. Better set filename to string.Empty before entering the loop.
Upvotes: 1