Пример #1
0
        public static async Task SetSettingToFileAsync <T>(string key, T value, bool roaming = false, Type[] extraTypes = null)
        {
            try
            {
                var file = roaming ? await ApplicationData.Current.RoamingFolder.CreateFileAsync(key + ".settings", CreationCollisionOption.ReplaceExisting) :
                           await ApplicationData.Current.LocalFolder.CreateFileAsync(key + ".settings", CreationCollisionOption.ReplaceExisting);

                var xml = DataContractSerialization.Serialize(value, true, extraTypes);
                await FileIO.WriteTextAsync(file, xml, Windows.Storage.Streams.UnicodeEncoding.Utf8);
            }
            catch (IOException ex)
            {
                Debug.WriteLine(ex.StackTrace);
            }
        }
Пример #2
0
        public static async Task <T> GetSettingFromFileAsync <T>(string key, T defaultValue, bool roaming = false, Type[] extraTypes = null)
        {
            try
            {
                var file = roaming ? await ApplicationData.Current.RoamingFolder.CreateFileAsync(key + ".settings", CreationCollisionOption.OpenIfExists) :
                           await ApplicationData.Current.LocalFolder.CreateFileAsync(key + ".settings", CreationCollisionOption.OpenIfExists);

                var xml = await FileIO.ReadTextAsync(file, Windows.Storage.Streams.UnicodeEncoding.Utf8);

                return(!String.IsNullOrEmpty(xml) ? DataContractSerialization.Deserialize <T>(xml, extraTypes) : defaultValue);
            }
            catch (IOException ex)
            {
                Debug.WriteLine(ex.StackTrace);
                return(defaultValue);
            }
        }