[RESOLVED] [2.0][2005] StreamReader Problem
Hi all,
I have the following code for returning an ArrayList from a function:
Code:
public ArrayList listFilesAndFolders(Uri uriLevel)
{
StreamReader reader = null;
ArrayList alFilesAndFolders = new ArrayList();
if (uriLevel.Scheme != Uri.UriSchemeFtp)
{
return null;
}
try
{
FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(getFTPPath(uriLevel.ToString()));
listRequest.Method = WebRequestMethods.Ftp.ListDirectory;
//listRequest.Credentials = new NetworkCredential(m_username, m_password);
listRequest.Credentials = m_networkcred;
FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse();
reader = new StreamReader(listResponse.GetResponseStream());
while (!reader.EndOfStream)
{
alFilesAndFolders.Add(reader.ReadLine());
}
listResponse.Close();
}
catch (UriFormatException ex)
{
Debug.Print(ex.Message);
return null;
}
catch (WebException ex)
{
Debug.Print(ex.Message);
return null;
}
finally
{
if (reader != null)
reader.Close();
}
return alFilesAndFolders;
}
I keep getting an error after the loop has been run through that reader has thrown an exception: System.ObjectDisposedException and it is caught by the code which calls the function. In the watch window when debugging it also confirms the exception is thrown. I cannnot see why the reader is disposed prior to me actually closing it. Can somebody please help and provide any suggestions.
Many thanks in advance,
Grant
Re: [2.0][2005] StreamReader Problem
I don't know this for a fact but here's my theory. You are creating your StreamReader from a response stream from your FtpWebResponse object. I'm betting that when you call Close on the FtpWebResponse it is Disposing that stream, so when you reach your 'finally' block the StreamReader variable is not a null reference but it is referring to an object whose underlying stream has been Disposed.