コード例 #1
0
        public static void SaveValue(string group, string key, object value)
        {
            Dictionary <string, Dictionary <string, object> > data = Persistence.ReadData();

            Dictionary <string, object> groupValues = null;

            if (value != null)
            {
                if (!data.TryGetValue(group, out groupValues))
                {
                    data[group] = new Dictionary <string, object>();
                }

                data[group][key] = value;
            }
            else
            {
                if (data.TryGetValue(group, out groupValues))
                {
                    data[group].Remove(key);
                    if (data[group].Count == 0)
                    {
                        data.Remove(group);
                    }
                }
            }

            XmlSerializer serializer = new XmlSerializer(typeof(List <PersistenceRow>));

            List <PersistenceRow> values = new List <PersistenceRow>();

            foreach (string groupKey in data.Keys)
            {
                foreach (string valueKey in data[groupKey].Keys)
                {
                    PersistenceRow dataRow = new PersistenceRow(groupKey, valueKey, data[groupKey][valueKey]);
                    values.Add(dataRow);
                }
            }

            string containerDir = Directory.GetParent(Persistence.location).FullName;

            if (!Directory.Exists(containerDir))
            {
                Directory.CreateDirectory(containerDir);
            }

            using (FileStream stream = File.Open(Persistence.location, FileMode.Create, FileAccess.Write))
            {
                serializer.Serialize(stream, values);
            }
        }
コード例 #2
0
        public static void SaveValue(string group, string key, object value)
        {
            Dictionary<string, Dictionary<string, object>> data = Persistence.ReadData();
            
            Dictionary<string, object> groupValues = null;

            if (value != null)
            {
                if (!data.TryGetValue(group, out groupValues))
                {
                    data[group] = new Dictionary<string, object>();
                }

                data[group][key] = value;
            }
            else
            {
                if (data.TryGetValue(group, out groupValues))
                {
                    data[group].Remove(key);
                    if (data[group].Count == 0)
                    {
                        data.Remove(group);
                    }
                }
            }

            XmlSerializer serializer = new XmlSerializer(typeof(List<PersistenceRow>));

            List<PersistenceRow> values = new List<PersistenceRow>();
            foreach (string groupKey in data.Keys)
            {
                foreach (string valueKey in data[groupKey].Keys)
                {
                    PersistenceRow dataRow = new PersistenceRow(groupKey, valueKey, data[groupKey][valueKey]);
                    values.Add(dataRow);
                }
            }

            string containerDir = Directory.GetParent(Persistence.location).FullName;

            if (!Directory.Exists(containerDir))
            {
                Directory.CreateDirectory(containerDir);
            }

            using (FileStream stream = File.Open(Persistence.location, FileMode.Create, FileAccess.Write))
            {
                serializer.Serialize(stream, values);
            }
        }