tags:

views:

56

answers:

0

I have a WPF application that serializes some settings to an XML file in Isolated Storage. I was playing around with different classes to find out which would properly serialize my information and ended up with a List of some custom class. This means I've changed the type used by the XmlSerializer class a couple of times in succession to get to the right result.

Now I'm having troubles because, whenever I load my settings I get the format I used before this, which is a List of KeyValuePairs (I know that doesn't work because it has read-only properties, but that is not the point right now). Saving settings seems to work like expected, until I try and load my settings again.

I've tried deleting the file from Isolated Storage and recreating it completely, but it doesn't change anything. I've tried using a different file name and then things get really weird. It saves fine to the new file, and when I try to load it again the GetFileNames method does return the filename I used, but when I try to open this file by creating a new IsolatedStorageFileStream with FileMode.Open it throws a FileNotFoundException!

Can anyone help me resolve these issues?

Below is the code I use for loading and saving.

        private void LoadSettings()
    {
        IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly();
        try
        {
            string[] fileNames = file.GetFileNames(SettingsFileName);
            if (fileNames.Length > 0)
            {
                IsolatedStorageFileStream stream = new IsolatedStorageFileStream(SettingsFileName, System.IO.FileMode.Open);
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<BindableString>));
                    List<BindableString> settings = serializer.Deserialize(stream) as List<BindableString>;
                    IEnumerable<BindableString> searchRootDirectorySettings =
                        settings.Where(setting => setting.Name.Equals(SearchRootDirectorySettingName));
                    if (searchRootDirectorySettings.Any())
                    {
                        _searchRootDirectory = searchRootDirectorySettings.First();
                    }
                    IEnumerable<BindableString> styleFilePathSettings =
                        settings.Where(setting => setting.Name.Equals(StyleFilePath));
                    if (styleFilePathSettings.Any())
                    {
                        _stylesFilePath = styleFilePathSettings.First();
                    }
                }
                finally
                {
                    stream.Close();
                }
            }
        }
        finally
        {
            file.Close();
        }
    }

    private void SaveSettings()
    {
        IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly();
        try
        {
            string[] fileNames = file.GetFileNames(SettingsFileName);
            if (fileNames.Length > 0)
            {
                file.DeleteFile(SettingsFileName);
            }
            IsolatedStorageFileStream stream = new IsolatedStorageFileStream(SettingsFileName, FileMode.Create, file);
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<BindableString>));
                List<BindableString> settings = new List<BindableString>();
                settings.Add(_searchRootDirectory);
                settings.Add(_stylesFilePath);
                serializer.Serialize(stream, settings);
            }
            finally
            {
                stream.Close();
            }
        }
        finally
        {
            file.Close();
        }