I'm developing a class that can send fttp requests, it has a utility method that can execute different types of ftp methods:
private FtpWebResponse DoFttpRequest(Uri uri, NetworkCredential credentials, string method, string file = null)
{
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = credentials;
request.Method = method;
if (!string.IsNullOrEmpty(file))
{
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
{
writer.Write(file);
}
}
return (FtpWebResponse)request.GetResponse();
}
As you can see, this methods executes ftp method and returns response stream to the caller. Here is the client method that uses this method to write string contents to a file through ftp:
public void WriteToFile(string path, string contents)
{
var uri = new Uri(path);
using (var ftpResponse = DoFttpRequest(uri, _credentials, Ftp.UploadFile, contents)) { }
}
As you can see, here I'm using empty using statement using (var ftpResponse = DoFttpRequest(uri, _credentials, Ftp.UploadFile, contents)) { }
to dispose of the received stream.
Is this a good approach to dispose object like that? Is it even necessary to dispose this stream, since it will probably be disposed by the garbage collector anyway?