Question

I am thinking of using following code, but I want to transfer hundreds of files and it does not look viable to connect and then disconnect on every file transfer.

request = (FtpWebRequest) FtpWebRequest.Create(FtpAddress + file);

request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(User, Pass);

request.UsePassive = IsPassive;
request.UseBinary = true;
request.KeepAlive = false;

FileStream fs = File.OpenRead("");
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();

Stream ftpStream = request.GetRequestStream();
ftpStream.Write(buffer, 0, buffer.Length);

ftpStream.Close();

What options do I have for uploading all of these files using a single connection?

Was it helpful?

Solution

I have not verified this to be true, but in my quick 30 second search, if you set

request.KeepAlive = true;

on every request you create except the last one, apparently only the first FTPWebRequest makes a full login connection.

Then when you create the last FTPWebRequest, set

request.KeepAlive = false;

and it will close the connection when done. You can verify this if you have access to the FTP server's logs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top