The IsolatedStorageSettings.Save method in Windows Phone: does it save the whole dictionary?

て烟熏妆下的殇ゞ 提交于 2019-12-25 12:11:42

问题


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.



来源:https://stackoverflow.com/questions/22384593/the-isolatedstoragesettings-save-method-in-windows-phone-does-it-save-the-whole

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!