The IsolatedStorageSettings.Save method in Windows

2019-09-07 03:14发布

问题:

Does the IsolatedStorageSettings.Save method in a Windows Phone application save the whole dictionary regardless of the changes we made in it? I.e. if we have say 50 items in it, and change just one, does the Save method saves (serializes, etc) the whole dictionary again and again? Is there any detailed documentation on this class and does anybody know what data storage format is used "under the hood"?

回答1:

I've managed to find the implementation of the IsolatedStorageSettings.Save method in the entrails of the Windows Phone emulator VHD images supplied with the Windows Phone SDK (the answer to this question on SO helped me to do that). Here is the source code of the method:

public void Save()
{
    lock (this.m_lock)
    {
        using (IsolatedStorageFileStream isolatedStorageFileStream = this._appStore.OpenFile(this.LocalSettingsPath, 4))
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                Dictionary<Type, bool> dictionary = new Dictionary<Type, bool>();
                StringBuilder stringBuilder = new StringBuilder();
                using (Dictionary<string, object>.ValueCollection.Enumerator enumerator = this._settings.get_Values().GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        object current = enumerator.get_Current();
                        if (current != null)
                        {
                            Type type = current.GetType();
                            if (!type.get_IsPrimitive() && type != typeof(string))
                            {
                                dictionary.set_Item(type, true);
                                if (stringBuilder.get_Length() > 0)
                                {
                                    stringBuilder.Append('\0');
                                }
                                stringBuilder.Append(type.get_AssemblyQualifiedName());
                            }
                        }
                    }
                }
                stringBuilder.Append(Environment.get_NewLine());
                byte[] bytes = Encoding.get_UTF8().GetBytes(stringBuilder.ToString());
                memoryStream.Write(bytes, 0, bytes.Length);
                DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(Dictionary<string, object>), dictionary.get_Keys());
                dataContractSerializer.WriteObject(memoryStream, this._settings);
                if (memoryStream.get_Length() > this._appStore.get_AvailableFreeSpace() + isolatedStorageFileStream.get_Length())
                {
                    throw new IsolatedStorageException(Resx.GetString("IsolatedStorageSettings_NotEnoughSpace"));
                }
                isolatedStorageFileStream.SetLength(0L);
                byte[] array = memoryStream.ToArray();
                isolatedStorageFileStream.Write(array, 0, array.Length);
            }
        }
    }
}

So, as we can see the whole dictionary is serialized every time when we call Save. And we can see from code what method is used to serialize the collection values.