By the way, here is the code I am using. This is a WPF application so I have implemented it as a backgroundworker task. Here are the pertinent methods that I am using:
void SendClick(object sender, RoutedEventArgs e)
{
// create a background worker thread and set its parameters
bgWorker = new BackgroundWorker();
bgWorker.WorkerReportsProgress = true;
bgWorker.WorkerSupportsCancellation = false;
bgWorker.DoWork += DoWork;
bgWorker.ProgressChanged += ProgressChanged;
bgWorker.RunWorkerCompleted += WorkerComplete;
SetStatus("Transferring data to the server...");
// start the background worker thread
bgWorker.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
FTPConnection ftp;
string zipFile;
FileStream stream;
BackgroundWorker worker = sender as BackgroundWorker;
bool uploadSucceded;
xferStatus = true;
ftp = new FTPConnection();
ftp.LocalDirectory = DIRECTORY;
// set log information
FTPConnection.LogFile = string.Format("ftp.log");
FTPConnection.LogLevel = LogLevel.All;
// set all of the FTP parameters
ftp.ServerAddress = "66.96.147.103";
// this is not the real login name and password...
ftp.UserName = "xxxx";
ftp.Password = "help me";
ftp.ConnectMode = FTPConnectMode.ACTIVE;
ftp.TransferType = FTPTransferType.BINARY;
ftp.AutoLogin = true;
ftp.Timeout = 30000;
ftp.UseGuiThreadIfAvailable = false;
// connect to the server
ftp.Connect();
if (ftp.IsConnected) {
// ftp.Uploaded += new FTPFileTransferEventHandler(FileUploadComplete);
for (int i = (numberPatients - 1); i >= 0; i--) {
zipFile = string.Format("{0}.zip", patients[i]);
worker.ReportProgress(0, string.Format("Transferring file {0}", zipFile));
stream = File.OpenRead(zipFile);
stream.Seek(0, SeekOrigin.Begin);
try {
uploadSucceded = true;
ftp.UploadStream(stream, zipFile);
} catch (Exception) {
// if we have an exception, then the upload failed
uploadSucceded = false;
}
if (uploadSucceded) {
// the upload was successful so delete the files
File.Delete(zipFile);
// clear the patient from the list & interface
patients[i] = string.Empty;
numberPatients--;
worker.ReportProgress(1);
// DisplayPatients();
} else {
xferStatus = false;
}
}
}
ftp.Close();
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
switch (e.ProgressPercentage) {
case 0: // update status message
SetStatus((string)e.UserState);
break;
case 1: // update patient display
DisplayPatients();
break;
default:
// do nothing in this case
break;
}
}
private void WorkerComplete(object sender, RunWorkerCompletedEventArgs e)
{
SetStatus(string.Format("File transfer completed {0}. Ready.",
xferStatus?"successfully":"unsuccessfully"));
UpdateControls();
UpdateDiskSpace();
DisplayPatients();