zoukankan      html  css  js  c++  java
  • wp8 入门到精通 Utilities类 本地存储+异步

    public class CCSetting
    {
    public async static void AddOrUpdateValue<T>(string key, T value)
    {
    try
    {
    if (key != null)
    {
    StorageFolder floder = ApplicationData.Current.LocalFolder;
    if (!(await floder.GetFoldersAsync()).Any(a => a.Name == "DrieSet"))
    {
    await ApplicationData.Current.LocalFolder.CreateFolderAsync("DrieSet");
    }
    string path = System.IO.Path.Combine("DrieSet", key);

    StorageFile file = await floder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);
    using (Stream stream = await file.OpenStreamForWriteAsync())
    {
    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(T));
    js.WriteObject(stream, value);
    }
    }
    }
    catch
    {

    }

    }
    public async static Task<T> GetValueOrDefault<T>(string key, T defaultValue)
    {
    try
    {
    StorageFolder floder = ApplicationData.Current.LocalFolder;
    string path = System.IO.Path.Combine("DrieSet", key);
    if (!(await floder.GetFoldersAsync()).Any(a => a.Name == "DrieSet"))
    {
    return defaultValue;
    }
    else
    {
    using (Stream istream = await floder.OpenStreamForReadAsync(path))
    {
    if (istream.Length != 0)
    {
    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(T));
    return (T)js.ReadObject(istream);
    }
    }
    }
    }
    catch { }
    return defaultValue;
    }

    public async static void RemoveKey(string key)
    {
    try
    {
    StorageFolder floder = ApplicationData.Current.LocalFolder;
    string path = System.IO.Path.Combine("DrieSet", key);
    if (await CheckFileExit(path))
    {
    StorageFile file = await floder.GetFileAsync(path);
    await file.DeleteAsync();
    }
    }
    catch { }
    }


    public async static Task<bool> CheckFileExit(string fileName)
    {
    bool b = false;
    if ((await ApplicationData.Current.LocalFolder.GetFoldersAsync()).Any(a => a.Name == "DrieSet"))
    {
    StorageFolder folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("DrieSet");
    b = (await folder.GetFilesAsync()).Any(a => a.Name == fileName);
    }
    else
    {
    await ApplicationData.Current.LocalFolder.CreateFolderAsync("DrieSet");
    b = false;
    }
    return b;
    }


    }

    internal class FolderHelper
    {
    const string PhoneStorage = "C:\Data\Users\Public\Pictures";
    const string SDCardStorage = "D:\";

    /// <summary>
    /// 获取手机存储设备的容量信息
    /// </summary>
    /// <param name="type">存储设备类型</param>
    /// <returns>存储设备的容量信息</returns>
    ///
    /// <exception cref="如果设备并不支持SD卡, 那么调用SD内存的时候会导致Exception, 记得try...catch..."/>
    public static async Task<StorageSpaceInfo> GetStorageSpaceInfo(StorageTypeEnum type)
    {
    string rootPath;
    switch (type)
    {
    case StorageTypeEnum.SDCard:
    rootPath = SDCardStorage;
    break;
    default:
    rootPath = PhoneStorage;
    break;
    }

    StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(rootPath);
    BasicProperties properties = await folder.GetBasicPropertiesAsync();
    IDictionary<string, object> filteredProperties =
    await properties.RetrievePropertiesAsync(
    new[] {
    "System.FreeSpace",
    "System.Capacity",
    "System.FileCount"
    });

    var capacity = filteredProperties["System.Capacity"];
    if (capacity == null)
    return null;

    var freeSpace = filteredProperties["System.FreeSpace"];
    if (freeSpace == null)
    return null;

    var fileCount = filteredProperties["System.FileCount"];


    return new StorageSpaceInfo((ulong)capacity, (ulong)freeSpace);
    }
    }

    /// <summary>
    /// 存储设备的容量信息
    /// </summary>
    internal class StorageSpaceInfo
    {
    public ulong CapacityByte { get; private set; }
    public ulong FreeSpaceByte { get; private set; }
    public ulong UsageByte { get; private set; }
    public string CapacityString { get; private set; }
    public string FreeSpaceString { get; private set; }
    public string UsageString { get; private set; }

    public StorageSpaceInfo(ulong capacity, ulong freeSpace)
    {
    CapacityByte = capacity;
    FreeSpaceByte = freeSpace;
    UsageByte = capacity - freeSpace;

    CapacityString = ConvertFileSize(CapacityByte);
    FreeSpaceString = ConvertFileSize(FreeSpaceByte);
    UsageString = ConvertFileSize(UsageByte);
    }

    /// <summary>
    /// 将以byte为单位的文件大小转换成合适的显示字符串
    /// 例:1024000byte -> 1000 KB
    /// </summary>
    /// <param name="size">文件大小 单位 : bytes</param>
    /// <returns></returns>
    string ConvertFileSize(double size)
    {
    string unit = "KB";
    double thresholdValue = 1024;

    double result = size / thresholdValue;

    //MB单位
    if (result > thresholdValue)
    {
    result = result / thresholdValue;
    unit = "MB";
    }

    //GB单位
    if (result > thresholdValue)
    {
    result = result / thresholdValue;
    unit = "GB";
    }

    //TB单位
    if (result > thresholdValue)
    {
    result = result / thresholdValue;
    unit = "TB";
    }

    return string.Format("{0} {1}", result.ToString("f2"), unit);
    }
    }

    enum StorageTypeEnum
    {
    Phone,
    SDCard
    }

  • 相关阅读:
    天梯赛练习2 补题
    QFNU 天梯赛练习 1 补题
    2019 山东省赛 B 题
    CCPC2020 网络赛 总结
    一个比较好看的 Typora 主题
    〔OS〕磁盘调度算法
    〔OS〕页面置换算法
    〔OS〕多线程模拟实现生产者和消费者
    〔OS〕银行家算法
    LCS and LIS
  • 原文地址:https://www.cnblogs.com/luquanmingren/p/3857364.html
Copyright © 2011-2022 走看看