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
You should dispose the reader before the filestream.
To simplify your code, you should make use of
usingblocks. They do the try/finally/dispose pattern automagically for you: