using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace DTSys.FTPDownloader
{
public class FileDownloader
{
static void Main(string[] args)
{
string ftpServer = "ftp:*******.com";
string userName = "*********.com";
string password = "********";
string destionationLocation = @"D:\project\ss\";
try
{
FileDownloader fDownloader = new FileDownloader();
List
if (collectionOfFiles != null)
{
Console.WriteLine("if statement");
Console.WriteLine(collectionOfFiles);
foreach (string fileName in collectionOfFiles)
{
Console.WriteLine("for statment");
string fullPathDestionation = Path.Combine(destionationLocation, Path.GetFileName(fileName));
fDownloader.downloadFile(ftpServer, fileName, userName, password, fullPathDestionation);
}
Console.WriteLine("if statement closed");
}
else
{
Console.WriteLine("function is not called");
}
Console.WriteLine("downloaded successfully");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public List
{
List
Console.WriteLine("getting filelist");
try
{
FtpWebRequest request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
FtpWebResponse response = request.GetResponse() as FtpWebResponse;
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
while (!reader.EndOfStream)
{
files.Add(reader.ReadLine());
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("files list got");
return files;
}
public void downloadFile(string FTPAddress, string filename, string username, string password, string destFile)
{
Console.WriteLine("Downloading files");
try
{
FtpWebRequest request = FtpWebRequest.Create(FTPAddress + "/"+filename) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
request.UsePassive = true;
request.UseBinary = false;
request.KeepAlive = false;
FtpWebResponse response = request.GetResponse() as FtpWebResponse;
Stream reader = response.GetResponseStream();
byte[] buffer = new byte[1024];
DateTime mod = response.LastModified;
DateTime obj = DateTime.Today;
int res1 = DateTime.Compare(mod, obj);
if (res1 != -1)
{
Console.WriteLine("DOWNLOAD IF STATEMENT");
using (Stream streamFile = File.Create(destFile))
{
Console.WriteLine("USING STATEMENT");
while (true)
{
Console.WriteLine("WHILE STATEMENT");
int bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead != 0)
{
Console.WriteLine("WHILE IF STATEMENT");
streamFile.Write(buffer, 0, bytesRead);
}
else
{
Console.WriteLine("WHILE ELSE STATEMENT");
break;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("downloaded");
}
}
}
In the above code i can download the files which are created with current date.. but the contents of files could not be downloaded.. Because im getting the value of bytesRead as 0.. but there is content in tat file.. i dont know why.. anybody can help me?
Sam HobbsPosted Feb 11, 2010, 10:47 PM
Then when you have that working, you can modify that test program to use the ftp site and credentials that you need, and get that working. Then merge that code into your main program. Note that in your program, you are setting:
Then you set:
That might be one reason the download is not working, but it is not the only reason.
Using:
And using the server as above, the following code downloads the file:
Sam HobbsPosted Feb 10, 2010, 7:13 PM