TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ila
NA
19.1k
0
listing files of a directory from ftp server..
Feb 2 2010 4:43 AM
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
{
//Create FTP request
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)
{
// write to log
}
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; //close the connection when done
//Streams
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)
{
// write to log
}
Console.WriteLine("downloaded");
}
I tried the above code to list the files of directory from ftp server and then download them in c#.. but the following code does not execute.. i dont know why.. can anybody help for me?
foreach (string fileName in collectionOfFiles)
{
Console.WriteLine("for statment");
string fullPathDestionation = Path.Combine(destionationLocation, Path.GetFileNameWithoutExtension(fileName) + ".*");
fDownloader.downloadFile(ftpServer, fileName, userName, password, fullPathDestionation);
}
Reply
Answers (
1
)
windows forms
total count of dropdown's selected