zoukankan      html  css  js  c++  java
  • UWP:本地应用数据

    获取应用的设置和文件容器

    使用 ApplicationData.LocalSettings 属性可以获取 ApplicationDataContainer 对象中的设置。

    注意:每个设置的名称最长可为 255 字符。每个设置的大小可以多达 8K 字节,而每个复合设置的大小可以多达 64K 字节。

    使用ApplicationData.LocalFolder 属性可以获取 StorageFolder 对象中的文件。

    下面是我封装的对应用数据操作的一个帮助类,主要围绕以上两个API:

       public class AppDataHelper
        {
            #region 字段
            /// <summary>
            /// 获取应用的设置容器
            /// </summary>
            private static Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    
            /// <summary>
            /// 获取独立存储文件
            /// </summary>
            private static Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            #endregion
    
            #region Set应用设置(简单设置,复合设置,容器中的设置)
            /// <summary>
            /// 简单设置
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public static void SetValue(string key, string value)
            {
                localSettings.Values[key] = value;
            }
    
            /// <summary>
            /// 复合设置
            /// </summary>
            /// <param name="composite"></param>
            public static void SetCompositeValue(Windows.Storage.ApplicationDataCompositeValue composite)
            {
                composite["intVal"] = 1;
                composite["strVal"] = "string";
    
                localSettings.Values["exampleCompositeSetting"] = composite;
            }
    
            /// <summary>
            /// 创建设置容器
            /// </summary>
            /// <param name="containerName"></param>
            /// <returns></returns>
            private static Windows.Storage.ApplicationDataContainer CreateContainer(string containerName)
            {
                return localSettings.CreateContainer(containerName, Windows.Storage.ApplicationDataCreateDisposition.Always);
            }
    
            /// <summary>
            /// 讲设置保存到设置容器
            /// </summary>
            /// <param name="containerName"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public static void SetContainerValue(string containerName, string key, string value)
            {
                if (!localSettings.Containers.ContainsKey(containerName))
                    CreateContainer(containerName);
    
                localSettings.Containers[containerName].Values[key] = value;
            }
            #endregion
    
            #region Get应用设置(简单设置,复合设置,容器中的设置)
    
            /// <summary>
            /// 获取应用设置
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public static object GetValue(string key)
            {
                return localSettings.Values[key];
            }
    
            /// <summary>
            /// 获取复合设置
            /// </summary>
            /// <param name="compositeKey"></param>
            /// <returns></returns>
            public static Windows.Storage.ApplicationDataCompositeValue GetCompositeValue(string compositeKey)
            {
                // Composite setting
                Windows.Storage.ApplicationDataCompositeValue composite =
                   (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values[compositeKey];
    
                return composite;
            }
    
            /// <summary>
            /// 从设置容器中获取应用设置
            /// </summary>
            /// <returns></returns>
            public static object GetValueByContainer(string containerName, string key)
            {
                bool hasContainer = localSettings.Containers.ContainsKey(containerName);
    
                if (hasContainer)
                {
                    return localSettings.Containers[containerName].Values.ContainsKey(key);
                }
                return null;
            }
            #endregion
    
            #region Remove已完成的设置
            /// <summary>
            /// 删除简单设置或复合设置
            /// </summary>
            /// <param name="key"></param>
            public static void Remove(string key)
            {
                localSettings.Values.Remove(key);
            }
    
            /// <summary>
            /// 删除设置容器
            /// </summary>
            /// <param name="key"></param>
            public static void RemoveContainer(string containerName)
            {
                localSettings.DeleteContainer(containerName);
            }
    
            #endregion
    
            #region 文件存储操作
    
            /// <summary>
            /// 写入文件
            /// </summary>
            public async void WriteTimestamp(string fileName,string contents)
            {
                try
                {
                    Windows.Storage.StorageFile sampleFile = await localFolder.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting);
                    await Windows.Storage.FileIO.WriteTextAsync(sampleFile, contents);
                }
                catch (Exception)
                {
                    throw;
                }
              
            }
    
            /// <summary>
            /// 读取文件
            /// </summary>
            public async Task<string> ReadTimestamp(string fileName)
            {
                try
                {
                    Windows.Storage.StorageFile sampleFile = await localFolder.GetFileAsync(fileName);
                    string contents = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
                    return contents;
                }
                catch (Exception)
                {
                    return "read faild";
                }
            }
            #endregion
        }
  • 相关阅读:
    printf()函数不能直接输出string类型
    HDU 6166.Senior Pan()-最短路(Dijkstra添加超源点、超汇点)+二进制划分集合 (2017 Multi-University Training Contest
    计蒜客 17119.Trig Function-切比雪夫多项式+乘法逆元 (2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F)
    POJ 1195.Mobile phones-二维树状数组
    HDU 1541.Stars-一维树状数组(详解)
    ACM中常见错误对应表
    HDU 6112.今夕何夕-蔡勒公式 (2017"百度之星"程序设计大赛
    hdu 2126 Buy the souvenirs 二维01背包方案总数
    codevs 1017 乘积最大 dp
    bzoj 2705: [SDOI2012]Longge的问题 欧拉函数
  • 原文地址:https://www.cnblogs.com/tonge/p/4760217.html
Copyright © 2011-2022 走看看