コード例 #1
0
        private static void Init()
        {
            if (m_Storage == null)
            {
                var storage = IsolatedStorageFile.GetUserStoreForApplication();

                if (storage.FileExists(ctFileName))
                {
                    using (var Guard = new MutexGuard(ctWriteMtxName))
                    {
                        if (!Guard.Lock(2000))
                        {
                            return;
                        }

                        using (var fs = storage.OpenFile(ctFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            DataContractSerializer serializer = new DataContractSerializer(typeof(SettingsList));

                            var    rdr = new StreamReader(fs);
                            string str = rdr.ReadToEnd();
                            fs.Position = 0;
                            try
                            {
                                m_Storage = (SettingsList)serializer.ReadObject(fs);
                            }
                            catch (System.Runtime.Serialization.SerializationException)
                            {
                                m_Storage = new SettingsList();
                            }
                        }
                    }
                }
                else
                {
                    m_Storage = new SettingsList();
                }
            }
        }
コード例 #2
0
        public static void Store()
        {
            if (m_Storage == null)
            {
                return;
            }

            var storage = IsolatedStorageFile.GetUserStoreForApplication();

            using (var Guard = new MutexGuard(ctWriteMtxName))
            {
                if (!Guard.Lock(2000))
                {
                    return;
                }

                // теперь настройки доступны только нам
                using (var fs = storage.OpenFile(ctFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(SettingsList));
                    serializer.WriteObject(fs, m_Storage);
                }
            }
        }