Tuesday, August 24, 2010

Get the File List of Files on FTP Server

public static FTPFileMetaData GetFTPFileList(string ftpServerIP, string ftpUserID, string ftpPassword, string strFolderName, string FileName)
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
FTPFileMetaData metaDataOfReFile = new FTPFileMetaData();
try
{
if (!ftpServerIP.Contains("ftp://"))
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
ftpServerIP + "/" + strFolderName + "/" + FileName));
}
else
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP +
"/" + strFolderName + "/" + FileName ));
}
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
//MessageBox.Show(reader.ReadToEnd());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
//MessageBox.Show(response.StatusDescription);
downloadFiles = result.ToString().Split('\n');


foreach (string s in downloadFiles)
{
metaDataOfReFile = GetMetaDataFTPFile(ftpServerIP, ftpUserID, ftpPassword, strFolderName, s);


#if (Diagnostics)

BAL.WriteTrackLog("Fetching meta data of FTP File in Re-DownloadMode - '" + s + "'" + " " + DateTime.Now);

#endif
}

return metaDataOfReFile;
}
catch (Exception ex)
{
BAL.WriteLog("Error in FTPModule for FTPList for ReDownload" + ex.ToString() + "-" + DateTime.Now);
return metaDataOfReFile;
}
}

No comments:

Post a Comment

 
Locations of visitors to this page