This is the method to download the files from ftp server:
public void DownloadFtpContent(object sender ,string file, string filesdirectories,string fn)
{
try
{
BackgroundWorker bw = sender as BackgroundWorker;
string filenameonly = Path.GetFileName(file);
string ftpdirectories = Path.Combine(ftpcontentdir, filesdirectories);
string fileurl = "ftp://" + file;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(fileurl);
reqFTP.Credentials = new NetworkCredential(UserName, Password);
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
reqFTP.KeepAlive = true;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.Proxy = null;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
if (!Directory.Exists(ftpdirectories))
{
Directory.CreateDirectory(ftpdirectories);
}
FileStream writeStream = new FileStream(ftpdirectories + "\\" + filenameonly, FileMode.Create);
string fnn = ftpdirectories + "\\" + filenameonly;
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
string SummaryText = String.Format("File Name {0} / {1}", "", filenameonly);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
var progress = bytesRead * 100.0 / writeStream.Length;
bw.ReportProgress((int)progress,SummaryText);
}
writeStream.Close();
response.Close();
}
catch (WebException wEx)
{
}
}
}
The method get each time a file from form1 backgroundworker dowork event. Then i'm reporting to the backgroundworker each file download progress and also the file name:
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
var progress = bytesRead * 100.0 / writeStream.Length;
bw.ReportProgress((int)progress,SummaryText);
}
And this is the backgroundworker prgoresschanged event:
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.toolStripStatusLabel2.Text = e.UserState.ToString();
this.toolStripProgressBar2.Value = Math.Min(this.toolStripProgressBar2.Maximum, e.ProgressPercentage);
}
Reporting the file name is fine. But the progress of each file download is not working good.
Each time a file is downloading i see the green color in the progressBar moving sometimes to 10% then back to 0 sometimes to 35% then back to 0 sometimes to 40% then back to 0 all the time it's moving some and then back to 0 it's never getting to the end to 100%
The problems is either with the progresschanged event with the way i'm getting the report:
this.toolStripProgressBar2.Value = Math.Min(this.toolStripProgressBar2.Maximum, e.ProgressPercentage);
Or maybe in the download method the way i'm calculating and reporting to the progressBar:
var progress = bytesRead * 100.0 / writeStream.Length;
bw.ReportProgress((int)progress,SummaryText);
The SummaryText reporting is working fine the problem is with the progress variable i think.