johny
johny

Reputation: 115

What is the right way to clean up resources after an FTP operation?

Once I upload a file using FTP, what should I do? I generally simply close the two streams, that is:

sourceStream.Close();
requestStream.Close();

What will be the effect be if they remain open? Will other users be able to login using the same credentials? Will I be able to login again?

Here is the full code for better understanding:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(
                          "bbbbb" + "bbbbbb" + "bbb/" + hj + "/" + hjj + ".txt");
request.Credentials = new NetworkCredential("bbbbb", "bbbbbb");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;

StreamReader sourceStream = new StreamReader(j + @"oo.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);

sourceStream.Close();
requestStream.Close();

Also, I have sometimes only the request.open method - which doesn't have any close() method assigned, and then I use request.Abort() at the very end of the FTP operation. Does that make any sense?

Upvotes: 4

Views: 3086

Answers (2)

ChrisWue
ChrisWue

Reputation: 19070

Well, the MSDN documentation states:

When using an FtpWebRequest object to upload a file to a server, you must write the file content to the request stream obtained by calling the GetRequestStream method or its asynchronous counterparts, the BeginGetRequestStream and EndGetRequestStream methods. You must write to the stream and close the stream before sending the request.

which indicates that bad things might happen if you don't close the request stream. If you don't close the source stream then you will very likely end up locking the file denying access for future request and/or other users - although it depends on the underlying OS and the sharing mode of the file.

As for closing the request: I haven't used WebRequest & Co much however following the above mentioned MSDN documentation it seems the pattern you are supposed to follow is

  • Create the request
  • Get the request stream
  • Write your data
  • Close the request stream
  • Get the response
  • Close the reponse object (which I assume finishes off the request)

It is probably a good idea to do so unless you know otherwise.

Upvotes: 1

user203570
user203570

Reputation:

Here's a way that's a little cleaner, uses fewer streams, and properly closes them when done.

Uri requestUri = new Uri(string.Concat("bbbbb", "bbbbbb", "bbb/", hj, "/", hjj, ".txt"));
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(requestUri);
request.Credentials = new NetworkCredential("bbbbb", "bbbbbb");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;

byte[] fileContents = File.ReadAllBytes(@"oo.txt");
request.ContentLength = fileContents.Length;

using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(fileContents, 0, fileContents.Length);
}

WebRequest.Abort is for terminating asynchronous operations, of which you have none. Don't call it here. Please investigate the using statement, which I included, as suggested in the comments by Mitch Wheat. It will automatically dispose of the stream object.

As for you other questions, of course someone can use the same credentials to log into the FTP server, but not as a result of your code. The same credentials will always log on successfully. I think you are worried about leaving the connection active, but that is a problem for the server to handle.

Upvotes: 0

Related Questions