public class FileDownloader
{
static void Main(string[] args)
{
string ftpServer = "ftp://ftp.******.com";
string userName = "*****";
string password = "******";
string destionationLocation = @"D:\project\ss";
FileDownloader fDownloader = new FileDownloader();
List<string> collectionOfFiles =
fDownloader.getFileList(ftpServer, userName, password);
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.GetFileNameWithoutExtension(fileName) + ".*");
fDownloader.downloadFile(ftpServer, fileName,
userName, password, fullPathDestionation);
}
Console.WriteLine("if statement closed");
}
else
{
Console.WriteLine("function is not called");
}
Console.WriteLine("downloaded successfully");
}
public List<string> getFileList(string FTPAddress, string username,
string password)
{
List<string> files = new List<string>();
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("fileslist 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.UsePassive = true;
request.UseBinary = false;
request.KeepAlive = false;
FtpWebResponse response = request.GetResponse()
as FtpWebResponse;
Stream reader = response.GetResponseStream();
byte[] buffer = new byte[1024];
using (Stream streamFile = File.Create(destFile))
{
while (true)
{
int bytesRead = reader.Read(buffer, 0,
buffer.Length);
if (bytesRead == 0)
{
break;
}
else
{
streamFile.Write(buffer, 0, bytesRead);
}
}
}
}
catch (Exception ex)
{
}
Console.WriteLine("downloaded");
}
foreach (string fileName in collectionOfFiles)
{
Console.WriteLine("for statment");
string fullPathDestionation =
Path.Combine(destionationLocation,
Path.GetFileNameWithoutExtension(fileName) + ".*");
fDownloader.downloadFile(ftpServer, fileName, userName,
password, fullPathDestionation);
}
When I debug this I got the following error:
Error: Object reference not set to an instance of an object.
pointing "in" in foreach statement
[edit]rjm: added <pre></pre> tags to clarify code.[/edit]