Downloading files from ftp site using Multithreading Technology
Downloading files from ftp site using multithreadin concept then we can downloading files at a time more no of number files.
Usage :
Quick download
code:
private void Form1_Load(object sender, System.EventArgs e)
{
Download_Connection_Read();
Download_Start();
}
private void Download_Connection_Read()
{
if ( File.Exists(Application.StartupPath + "\\Connection.ini"))
{
string newline;
StreamReader sr1 = new StreamReader(Application.StartupPath + "\\Connection.ini");
while ((newline = sr1.ReadLine()) != null)
{
string[] str = newline.Split('|');
if (str[0].ToLower() == "host")
downobj.HostAddress = str[1];
if (str[0].ToLower() == "login")
downobj.Login = str[1];
if (str[0].ToLower() == "password")
downobj.Password = str[1];
if (str[0].ToLower() == "protocol")
downobj.ProtoType = str[1];
}
}
}
public void Download_Start()
{
MySite.Host = downobj.HostAddress;
MySite.Login = downobj.Login;
MySite.Password = downobj.Password;
MySite.Protocol = downobj.ProtoType;
MySite.Connect();
if (MySite.IsConnected != 0)
{
Download_Process();
this.Close();
}
else
{
MySite.Disconnect();
}
}
public void Download_Process()
{
int counter = 0;
MySite.RemoteFolder = downobj.RemotePath + "/";
Getfiles = Application.StartupPath + @"\" + "Getfiles.txt";
if (File.Exists(Getfiles))
MySite.GetList(MySite.RemoteFolder, Getfiles, "%NAME/%SIZE");
if (File.Exists(Getfiles))
{
FileStream fs = new FileStream(Getfiles, FileMode.Open, FileAccess.Read);
StreamReader downloadedreader = new StreamReader(fs);
FileStream fszip = new FileStream(Getfiles, FileMode.Open, FileAccess.Read);
StreamReader zipreader = new StreamReader(fszip);
string[] xmlfile;
string[] xmlfilename;
string[] zipfile;
#region begins Read journal.ini file and compare with ftp files
while (downloadedreader.Peek()>0)
{
xmlfile = downloadedreader.ReadLine().ToString().Split('/');
xmlfilename = xmlfile[0].ToString().Split('.');
zipfile = zipreader.ReadLine().Split('/');
zipfile = zipreader.ReadLine().ToString().Split('/');
if (zipfile.Length==2)
{
strContent = downloadedreader.ReadLine().Split('.');
ss = strContent[0].Split('_');
JournalObj jobj1 = new JournalObj();
jobj1.Journal = ss[0].ToString();
jobj1.FileMode = ss[1].ToString();
deliverytype = ss[2].ToString();
jobj1.ArticleId = ss[3];
jobj1.ProductionStage = ss[4];
jobj1.FileName = strContent[0];
jobj1.Size = long.Parse (zipfile[1].ToString ());
StreamReader journalreader = new StreamReader(Application.StartupPath + "\\Journal.ini");
while ((journalcontent = journalreader.ReadLine()) != null)
{
journaltxt = journalcontent.ToString().Split('|');
if (journaltxt[0] == jobj1.Journal)
{
jobj1.Journal = journaltxt[1];
jobj1.Filecount = counter;
counter++;
FtpList.Add(jobj1);
}
}
}
}
#endregion
if (FtpList.Count>0)
{
int threadcount = -1;
int downloadcount =FtpList.Count;
Download[] downloadprocess = new Download[downloadcount];
Thread[] ts = new Thread[downloadcount];
foreach (JournalObj ftp in FtpList)
{
if (ftp.FileName.ToString()!=null)
{
threadcount++;
downloadprocess[ftp.Filecount] = new Download (MySite, ftp.FileName,
downobj.RemotePath,
ftp.Multiparts,ftp.Size);//set values to constructor function
ts[ftp.Filecount] = new Thread(new ThreadStart(downloadprocess[ftp.Filecount].DownLoadFile));//invoke download process
ts[ftp.Filecount].Start();
}
}
List
Boolean result = true;
int No_of_tries = 0;
//while (result && No_of_tries < 3)
while(FtpDownload.Count!=0)
{
List
for (int itemcount = 0; itemcount < FtpDownload.Count; itemcount++)
{
ts[FtpDownload[itemcount].Filecount].Join();
Thread.Sleep(10000);
FtpFinished.Add(downloadprocess[FtpDownload[itemcount].Filecount].GetStatus(FtpDownload[itemcount]));//get the status of the article
}
No_of_tries++;
result = false;
for (int itemcount = 0; itemcount < FtpFinished.Count; itemcount++)
{
if (FtpFinished[itemcount].Status.Equals("TRANSFERRING"))
result = true;
if (FtpFinished[itemcount].Status.Equals("FINISHED") ||
FtpFinished[itemcount].Status.Equals("ERROR"))
{
FtpFinished.Remove(FtpFinished[itemcount]);
itemcount = -1;
}
else
result = true;
}
FtpDownload = new List
}
}
}
Download.cs
-----------
public Download(CuteFTPPro.TEConnection cute,
string Filename, string Pat, Int32 multiparts,long Size)
{
this.MySite=cute;
this.Filename = Filename;
this.Size = Size;
this.Pat = Pat;
this.multipart = multiparts;
}
internal void DownLoadFile()
{
try
{
source = MySite.RemoteFolder + "/" + Filename;
LocalDestination = "d:\\djs_download\\downloaded files\\";
string[] split_path = Pat.Split('/');
if (!Directory.Exists(LocalDestination))
Directory.CreateDirectory(LocalDestination);
try
{
MySite.DownloadAsync(source.Replace(".zip", "") + ".*", LocalDestination, this.multipart); //downloading the file
}
catch (Exception ex)
{
StreamWriter stw = new StreamWriter(Application.StartupPath+"\\" + "LOG FILES\\" + "DownloadError.Log", true);
stw.WriteLine("Xml File Problem :"+"\t"+ source.ToString() +"\t" + ex.Message.ToString());
stw.Close();
}
}
catch
{
}
}
internal JournalObj GetStatus(JournalObj ftpf)
{
tomailid = null; ccmailid = null; bccemailid = null;
ftpf.Status = "Pending";
try
{
FileInfo DownloadedFile = new FileInfo(LocalDestination + Filename+".zip");
long Size1 = DownloadedFile.Length;
long Size2 =Size;
if (Size1 == Size2)
{
try
{
ftpf.Status = "OVER";
if (MySite.get_Status(MySite.AsyncTaskNumber - 1).ToUpper().Equals("ERROR"))
{
ftpf.Status = "ERROR";
}
else if (MySite.get_Status(MySite.AsyncTaskNumber - 1).Equals("TRANSFERRING"))
ftpf.Status = "TRANSFERRING";
else if (MySite.get_Status(MySite.AsyncTaskNumber - 1).Equals("FINISHED"))
{
ftpf.Status = "FINISHED";
}
}
catch
{
}
}
else
{
string temp = MySite.get_Status(MySite.AsyncTaskNumber - 1).ToString();
if (MySite.get_Status(MySite.AsyncTaskNumber - 1).ToUpper().Equals("TRANSFERRING"))
{
ftpf.Status = "TRANSFERRING";
}
else
if (MySite.get_Status(MySite.AsyncTaskNumber - 1).ToUpper().Equals("ERROR"))
{
ftpf.Status = "ERROR";
}
//ftpf.Status = "Pending";
//if (MySite.get_Status(MySite.AsyncTaskNumber - 1).ToUpper().Equals("ERROR") ||
//MySite.get_Status(MySite.AsyncTaskNumber - 1).ToUpper().Equals("FINISHED"))
//{
// ftpf.Status = "ERROR";
//}
}
if (ftpf.Status.ToUpper().Equals("FINISHED"))
{
List
List
foreach (ZipEntry zipin in zipfiledetails)
{
FileDetails.Add ( zipin.getName().ToString());
}
ReadXml();
SendMail(FileDetails);
}
}
catch (Exception ex)
{
if (MySite.get_Status(MySite.AsyncTaskNumber - 1).ToUpper().Equals("ERROR") ||
MySite.get_Status(MySite.AsyncTaskNumber - 1).ToUpper().Equals("FINISHED"))
{
ftpf.Status = "ERROR";
StreamWriter stw = new StreamWriter(Application.StartupPath+"\\" + "LOG FILES\\" + "DownloadError.Log", true);
stw.WriteLine("Xml File Problem : " + "\t" +ex.Message.ToString());
stw.Close();
}
}
finally
{
}
return ftpf;
}
private void ReadXml()
{
try
{
XmlTextReader tr = new XmlTextReader("d:\\djs_download\\Downloaded Files\\" + Filename + ".xml");
while (tr.Read())
{
if (tr.NodeType == XmlNodeType.Element)
{
if (tr.Name.ToLower().Equals("to_email"))
tomailid = tr.ReadString();
else
if (tr.Name.ToLower().Equals("cc_email"))
ccmailid = tr.Value.ToString();
else
if (tr.Name.ToLower().Equals("bcc_email"))
bccemailid = tr.Value.ToString();
}
}
tr.Close();
}
catch(XmlException xmlex)
{
StreamWriter stw=new StreamWriter ( Application.StartupPath+"\\" + "LOG FILES\\" + "XmlError.Log",true);
stw.WriteLine("Xml File Problem : "+"\t"+Filename+"\t" + xmlex.Message.ToString());
stw.Close();
}
}
hi PrabaKaran
u done a nice job
it is very useful
thanks,
regards
prakash