1

I am using this code to read values from an isolated storage

        IsolatedStorageFile isoStore = null;
        StreamReader reader = null;
        IsolatedStorageFileStream isolatedStorageFileStream = null;
        String strIsolatedStorageValue = string.Empty;

        isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

        try
        {
            isolatedStorageFileStream = new IsolatedStorageFileStream(strKey + ".txt", FileMode.OpenOrCreate, isoStore);
            // This code opens the store and reads the string.
            reader = new StreamReader(isolatedStorageFileStream);

            // Read a line from the file and add it to sb.
            strIsolatedStorageValue = reader.ReadLine();
        }
        catch (Exception)
        {

        }
        finally
        {
            if (isolatedStorageFileStream != null)
            {
                isolatedStorageFileStream.Dispose();
            }
            if (reader != null)
            {
                reader.Dispose();
            }
        }

        // Return the string.
        return strIsolatedStorageValue;

The problem is that when I am disposing isolatedStorageFileStream and then disposing the reader, visual studio tells me that isolatedStorageFileStream could be disposed more than once! and when not disposing it I am getting the warning that isolatedStorageFileStream should be disposed first.

What to do in such a case, that is disposing an object used in the constructor of another disposable object

Thanks

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
user1010572
  • 469
  • 1
  • 4
  • 16

3 Answers3

1

Use the using keyword:

using (IsolatedStorageFileStream isolatedStorageFileStream = 
       new IsolatedStorageFileStream(
            strKey + ".txt", FileMode.OpenOrCreate, isoStore))
using (StreamReader reader = new StreamReader(isolatedStorageFileStream))
{
    // Read a line from the file and add it to sb.
    strIsolatedStorageValue = reader.ReadLine();
}

return strIsolatedStorageValue;

using safely calls Dispose for you and you don't have to call it manually.

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
1

You should dispose the reader before the filestream.

To simplify your code, you should make use of using blocks. They do the try/finally/dispose pattern automagically for you:

using (isolatedStorageFileStream = new IsolatedStorageFileStream(
          strKey + ".txt", FileMode.OpenOrCreate, isoStore)) {
    // This code opens the store and reads the string.
    using (reader = new StreamReader(isolatedStorageFileStream)) {
        strIsolatedStorageValue = reader.ReadLine();
   }
}
Jan
  • 15,802
  • 5
  • 35
  • 59
  • Thanks :), but I am still gwetting the warning from visual studio like this:Warning 5 CA2202 : Microsoft.Usage : Object 'isolatedStorageFileStream' can be disposed more than once in method 'IsolatedStorageReader.ReadFromIsolatedStorage(string)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object – user1010572 Feb 06 '12 at 08:53
  • Its just a fxcop warning. See this answer for details: http://stackoverflow.com/a/3831947/25727 – Jan Feb 06 '12 at 09:02
  • By the way, you can decorate the StreamReader class instance using [NonClosableStream class instance](http://code.google.com/p/sharpfilesystem/source/browse/trunk/SharpFileSystem/IO/NonClosableStream.cs) to prevent closing the stream by StreamReader class instance. – Sergey Vyacheslavovich Brunov Feb 06 '12 at 09:35
1

The using statement automatically does try-finally for IDisposable (dispose if not null) for you.

using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(strKey + ".txt", FileMode.OpenOrCreate, isoStore))
{
    using (StreamReader reader = new StreamReader(isolatedStorageFileStream))
    {
        string strIsolatedStorageValue = reader.ReadLine();
        return strIsolatedStorageValue;
    }
}