Results 1 to 2 of 2

Thread: [RESOLVED] [2.0][2005] StreamReader Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Bonny Scotland
    Posts
    141

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width