.Net ramblings
# Friday, 17 February 2006
FtpWebRequest with ProgressBar [C# Ftp]
Screenshot of upload operationhi,
i was delighted to see .Net 2.0 has an excellent class for working with FTP.  looking back i can't believe it wasn't part of 1.1. 

i noticed there was no built-in support for reporting the progress of an FTP operation to a user interface.  I needed this functionality in an app i'm working on so i built a class and i'm posting it here in case anyone is interested.

The class FtpProgress inherits from BackgroundWorker, so you just drag an FtpProgress component onto your form, hook up events for Completed and ProgressChanged, and your laughing.  There is a mini class called FtpSettings which stores all the connect info etc to pass in as a paramter to the RunWorkerAsync() method.

I have a variable called 'ChunkSize' which determines the size of each buffer written to the FtpWebRequest output stream.  It is set to 4k by default, and you can increase this if you want for very fast networks but i don't think it will make much difference.  I think the overhead is negligible.

Download the source code and demo application [30 k].

It only does upload, so if you want download functionality, you can do the mirror-image of the upload code, i.e. instead of writing buffers to the output stream, read it from the input stream, or however that would work :) 

here is all the code you need to start an Upload, and report progress:
private void btnUpload_Click(object sender, EventArgs e)
// create a new FtpSettings class to store all the paramaters for the FtpProgress thread
FtpSettings f = new FtpSettings();
f.Host = this.txtHost.Text;
f.Username = this.txtUsername.Text;
f.Password = this.txtPassword.Text;
f.TargetFolder = this.txtDir.Text;
f.SourceFile = this.txtUploadFile.Text;
f.Passive = this.chkPassive.Checked;
f.Port = Int32.Parse(this.txtPort.Text);

private void ftpProgress1_ProgressChanged(object sender, ProgressChangedEventArgs e)
this.toolStripStatusLabel1.Text = e.UserState.ToString(); // the message will be something like: 45 Kb / 102.12 Mb
this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Maximum, e.ProgressPercentage);

Friday, 17 February 2006 18:09:05 (GMT Standard Time, UTC+00:00)  #    Comments [29]  .Net General | .Net Windows FormsTracked by:
"flagyl side effects how long" (flagyl side effects how long) [Trackback]
"scuola lingua" (scuola lingua) [Trackback]
"birdhouse distributors" (birdhouse distributors) [Trackback]
"asiatiche merda nella stanza" (asiatiche merda nella stanza) [Trackback]
"palm scheduler appointments" (palm scheduler appointments) [Trackback]
"business cards" (business cards) [Trackback]
"bed and breakfast positano" (bed and breakfast positano) [Trackback]
"attivita commerciale" (attivita commerciale) [Trackback]
"blue cross of ca" (blue cross of ca) [Trackback]
"mouse pad" (mouse pad) [Trackback]
"I Want to Play the Price Is Right" (I Want to Play the Price Is Right) [Trackback]
"plants retail frisco%2c tx" (plants retail frisco%2c tx) [Trackback]
"street racing videos" (street racing videos) [Trackback]
"limousine stripper" (limousine stripper) [Trackback]
"altro consumo it" (altro consumo it) [Trackback]
"lake compounce" (lake compounce) [Trackback]
"amore segreto" (amore segreto) [Trackback]
"amateur adult mpeg archives" (amateur adult mpeg archives) [Trackback]
"printing magnetic business cards" (printing magnetic business cards) [Trackback]
"girls gone wild" (girls gone wild) [Trackback]
"volagratis" (volagratis) [Trackback]
"song lyrics riding dirty" (song lyrics riding dirty) [Trackback]
"handsome teen scopata" (handsome teen scopata) [Trackback]
"fast weight loss wellbutrin" (fast weight loss wellbutrin) [Trackback]
"ridicolo fighetta fotti" (ridicolo fighetta fotti) [Trackback]
"free teen sex video" (free teen sex video) [Trackback]
"citizen wrist watch" (citizen wrist watch) [Trackback]
"ragazza ginevra" (ragazza ginevra) [Trackback]
"raven" (raven) [Trackback]
"cpa client newsletters" (cpa client newsletters) [Trackback]