Hi Experts,
I have code snippet for List the FTP , we need correct that code to work in fast, currently its working perfect , but slow.
{codecitation class="brush: csharp; gutter: true;" width="650px"}
using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.Collections;
public partial class GetFTPFileInfo : System.Web.UI.Page
{
string ftpServerIP;
string ftpUserID;
string ftpPassword;
private string _fileName = string.Empty;
// Represents the filename without extension
private string _fileExtension = string.Empty; // Represents the file extensionprivate
string _path = string.Empty; // Represents the complete path
private DirectoryEntryType _fileType; // Represents if the current listing represents a file/directory.
private long _size; // Represents the size.
private DateTime _fileDateTime; // DateTime of file/Directory
private string _permissions; // Permissions on the directory
private string _fullName;
IFormatProvider culture = System.Globalization.CultureInfo.InvariantCulture;
public Int64 TotalFiles=0;
# region Public Properties
public string FileName
{
get { return _fileName; }
set
{
_fileName = value;// Set the FileExtension.
if (_fileName.LastIndexOf(".") > -1)
{ FileExtension = _fileName.Substring(_fileName.LastIndexOf(".") + 1); }
}
}
public string FileExtension
{
get { return _fileExtension; }
set { _fileExtension = value; }
}
public string FullName
{
get { return _fullName; }
set { _fullName = value; }
}
public string Path { get { return _path; } set { _path = value; } }
internal DirectoryEntryType FileType;
public long Size { get { return _size; } set { _size = value; } }
public DateTime FileDateTime { get { return _fileDateTime; } set { _fileDateTime = value; } }
public string Permissions { get { return _permissions; } set { _permissions = value; } }
public string NameOnly { get { int i = this.FileName.LastIndexOf("."); if (i > 0)return this.FileName.Substring(0, i); else return this.FileName; } }
public enum DirectoryEntryType { File, Directory }
#endregion
internal string[] _ParseFormats = { @"(?[\-d])(?([\-r][\-w][\-xs]){3})\s+\d+\s+\w+\s+\w+\s+(?\d+)\s+(?\w+\s+\d+\s+\d{4})\s+(?.+)", @"(?[\-d])(?([\-r][\-w][\-xs]){3})\s+\d+\s+\d+\s+(?\d+)\s+(?\w+\s+\d+\s+\d{4})\s+(?.+)", @"(?[\-d])(?([\-r][\-w][\-xs]){3})\s+\d+\s+\d+\s+(?\d+)\s+(?\w+\s+\d+\s+\d{1,2}:\d{2})\s+(?.+)", @"(?[\-d])(?([\-r][\-w][\-xs]){3})\s+\d+\s+\w+\s+\w+\s+(?\d+)\s+(?\w+\s+\d+\s+\d{1,2}:\d{2})\s+(?.+)", @"(?[\-d])(?([\-r][\-w][\-xs]){3})(\s+)(?(\d+))(\s+)(?(\w+\s\w+))(\s+)(?(\d+))\s+(?\w+\s+\d+\s+\d{2}:\d{2})\s+(?.+)", @"(?\d{2}\-\d{2}\-\d{2}\s+\d{2}:\d{2}[Aa|Pp][mM])\s+(?\<\w+\>){0,1}(?\d+){0,1}\s+(?.+)", @"([]*\d{2}\-\d{2}\-\d{2}\s+\d{2}:\d{2}[Aa|Pp][mM])\s+([]*\<\w+\>){0,1}([]*\d+){0,1}\s+([]*.+)" };
BALEMail.BGetFTPFileDetails objFTPDetail = new BALEMail.BGetFTPFileDetails();
protected void Page_Load(object sender, EventArgs e)
{
ftpServerIP = "";
ftpUserID = "";
ftpPassword = "";
}
private string[] GetFilesDetailList()
{
string[] downloadFiles;
try
{
StringBuilder result = new StringBuilder();
FtpWebRequest ftp;
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = ftp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
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();
return result.ToString().Split('\n');
//MessageBox.Show(result.ToString().Split('\n'));
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
public string[] GetFileList(string path)
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
string[] result1 = null;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
path));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID,
ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
StreamReader reader = new StreamReader(response
.GetResponseStream(), System.Text.Encoding.UTF8);
result1 = (reader.ReadToEnd().Split('\n'));
//string line = reader.ReadLine();
//while (line != null)
//{
// if (line.Contains("DIR") == true)
// {
// result.Append(line);
// result.Append("\n");
// line = reader.ReadLine();
// }
//}
// result.Remove(result.ToString().LastIndexOf("\n"), 1);
reader.Close();
response.Close();
return result1;
// to remove the trailing '\n'
//result.Remove(result.ToString().LastIndexOf('\n'), 1);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
void FinalToGetFolder()
{
ArrayList DirectoriesList = new ArrayList();
ArrayList DirectoriesList1 = new ArrayList();
string currentDirectory = string.Empty;
string dtdate;
DateTime dtNow;
dtdate = DateTime.Now.ToString("dd/mm/yy");
dtNow = DateTime.Now.AddDays(-1);
DirectoriesList.Add("ftp://" + ftpServerIP);
// done:
for (int directoryCount = 0; directoryCount < DirectoriesList.Count; directoryCount++)
{
currentDirectory = DirectoriesList[directoryCount].ToString() + "/";
if (currentDirectory.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase) == false)
{
currentDirectory = string.Concat("ftp://" + ftpServerIP, currentDirectory);
}
string[] directoryInfo = GetFileList(currentDirectory);
if (directoryInfo != null)
{
for (int counter = 0; counter < directoryInfo.Length; counter++)
{
string currentFileOrDir = directoryInfo[counter];
if (currentFileOrDir.Length < 1)
// If all entries were scanned then break
{ break; }
currentFileOrDir = currentFileOrDir.Replace("\r", "");
GetFtpFileInfo(currentFileOrDir, currentDirectory);
if (FileType == DirectoryEntryType.Directory)
{
if (FullName.Contains("..") == false && FullName.Contains(".") == false)
{
DirectoriesList.Add(Path + FullName); //If Directory add to the collection.
//goto done;
}
}
}
}
}
ListBox1.Items.Clear();
foreach (string filename in DirectoriesList1)
{
ListBox1.Items.Add(filename);
}
}
private Match GetMatchingRegex(string line) { Regex regExpression; Match match; int counter; for (counter = 0; counter < _ParseFormats.Length - 1; counter++) { regExpression = new Regex(_ParseFormats[counter], RegexOptions.IgnoreCase); match = regExpression.Match(line); if (match.Success)return match; } return null; }
public void GetFtpFileInfo(string line, string path)
{
string directory = "";
Match match = GetMatchingRegex(line); //Get the match of the current listing.
// if (match != null)
// throw new Exception("Unable to parse the line " + line);
//{
// _fileName = match.Groups["name"].Value; //Set the name of the file/
_path = path; // Set the path from which the listing needs to be obtained.
// _permissions = match.Groups["permission"].Value; // Set the permissions available for the
// listingdirectory = match.Groups["dir"].Value;//Set the filetype to either Directory or File basing on the listing.
if (line.Contains("DIR") == true)
{
_fileType = DirectoryEntryType.Directory;
FileType = _fileType;
string[] arr;
arr = line.Split('>');
FullName = arr[1].Replace("")
}
else
{
_fileType = DirectoryEntryType.File; //_size = long.Parse(match.Groups["size"].Value, culture);
//}try{_fileDateTime = //DateTime.Parse(match.Groups["timestamp"].Value, culture); // Set the datetime of the listing.
// }catch{_fileDateTime = DateTime.Now;}
// Initialize the readonly properties.
FileName = _fileName;
string[] arr;
if (line.Contains("") == true)
{
arr = line.Split('>');
FullName = arr[1].Replace(" | |
|