Q:

Strange issue with IsolatedStorage in WP7 device

I have a strange problem in my Windows Phone 7 application. I need to read/write some xml file in my app and I'm using IsolatedStorage to collect data. My app sends/gets data from SkyDrive this is why I use it.
Ok, here is function which generate exception:
private void CreateFileIntoIsolatedStorage(List<Record> list)
    {
        isf = IsolatedStorageFile.GetUserStoreForApplication();
        if(list.Count == 0)
            list = new List<Record>() { new Record { Date = DateTime.Today, Value = 0 }};

        if (isf.FileExists(fileName))
        {
            isf.DeleteFile(fileName);
        }

        XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
        xmlWriterSettings.Indent = true;

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(fileName, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Record>));
                using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                {
                    serializer.Serialize(xmlWriter, list);
                }
            }
        }
    }

Problem:
My problem starts when I this function runs for the second time. Then isf.DeleteFile(fileName); throws IsolatedStorageException. And creating stream crashed application.
It's strange cause it happens every time I run it on my device, and rarely when I use the debugger.
So my question is how can I solve it or are there better ways to do this?
Any help would be appreciated.

A:

Possibly it's because at the beginning of your method you have:
isf = IsolatedStorageFile.GetUserStoreForApplication();

And you never dispose of that. Then, later, you get it again in the using. But that one's disposed. And then the next time you call CreateFileIntoIsolatedStorage, you get it again, again without disposing.
Perhaps this is what you want:
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    if(list.Count == 0)
        list = new List<Record>() { new Record { Date = DateTime.Today, Value = 0 }};

    if (isf.FileExists(fileName))
    {
        isf.DeleteFile(fileName);
    }
}

Although that class-scoped isf variable is troublesome. If you want to keep the store active, then just call it once and leave it open. Otherwise, ditch the class-scoped variable.
Or, it might be due to this, from the documentation for IsolatedStorageFile.DeleteFile?

File deletions are subject to intermittent failures because files can be in use simultaneously by operating system features such as virus scanners and file indexers. This is especially true for recently created files. Macintosh users should be aware of this issue because of its frequent indexing.
  For these reasons, it is important to add code to the code block that handles the IsolatedStorageException to retry deleting the file or log a failure.

I would suggest something like:
int retryCount = 0;
while (retryCount < MaxRetryCount && isf.FileExists(fileName))
{
    try
    {
        isf.DeleteFile(fileName);
    }
    catch (IsolatedStorageException)
    {
        ++retryCount;
        // maybe notify user and delay briefly
        // or forget about the retry and log an error. Let user try it again.
    }
}

