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
}

wp8 入门到精通 Utilities类 本地存储+异步的更多相关文章

  1. 微信小程序入门一: 简易form、本地存储

    实例内容 登陆界面 处理登陆表单数据 处理登陆表单数据(异步) 清除本地数据 实例一: 登陆界面 在app.json中添加登陆页面pages/login/login,并设置为入口. 保存后,自动生成相 ...

  2. wp8 入门到精通 虚拟标示符 设备ID

    //获得设备虚拟标示符 wp8 public string GetWindowsLiveAnonymousID() { object anid = new object(); string anony ...

  3. wp8 入门到精通

    <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal" ...

  4. wp8 入门到精通 仿美拍评论黑白列表思路

    static bool isbool = false; private void BindGameDelete() { Tile tile = new Tile(); List<Color> ...

  5. wp8 入门到精通 生命周期

  6. wp8 入门到精通 定时更新瓷贴

    public class ScheduledAgent : ScheduledTaskAgent { static ScheduledAgent() { Deployment.Current.Disp ...

  7. wp8 入门到精通 ImageCompress 图片压缩

    //实例化选择器 PhotoChooserTask photoChooserTask = new PhotoChooserTask(); BitmapImage bimg; int newPixelW ...

  8. wp8 入门到精通 Gallery

    <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.Resources> ...

  9. wp8 入门到精通 MultiMsgPrompt

    List<NotifyMsg> arraymsg = new List<NotifyMsg>(); List<NotifyInfo> ArrayNotifyInfo ...

随机推荐

  1. 获取编辑框字符串,传入Intent

    private EditText etData; etData = (EditText) findViewById(R.id.etData); Intent i = new Intent(MainAc ...

  2. 图解SSL/TLS协议(HTTPS的安全层)

    http://blog.csdn.net/wallezhe/article/details/50977337 图解SSL/TLS协议     作者: 阮一峰 日期: 2014年9月20日 本周,Clo ...

  3. ASM:《X86汇编语言-从实模式到保护模式》第8章:实模式下硬盘的访问,程序重定位和加载

        第八章是一个非常重要的章节,讲述的是实模式下对硬件的访问(这一节主要讲的是硬盘),还有用户程序重定位的问题.现在整理出来刚好能和保护模式下的用户程序定位作一个对比. ★PART1:用户程序的重 ...

  4. Mac会给你一些欣喜

    Mac会给你一些欣喜 以前一直没有用过Mac,一直都是用Windows的电脑,只是偶尔会去用Ubuntu这样的Linux系统.Mac OS 确实是一只可以给你欣喜的系统. 上周拿到公司分发的Mac,到 ...

  5. 【Python】range和xrange区别

    转自:http://www.cnblogs.com/zhangjing0502/archive/2012/05/16/2503880.html range    函数说明:range([start,] ...

  6. LeetCode 242 Valid Anagram

    Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...

  7. 电子现金、电子钱包、qPBOC、闪付、UPCash

    一.关于金融IC卡领域的规范 由Europay.Mastercard.Visa三大国际信用卡组织联合制定的金融集成电路(IC)卡金融支付标准,称为EMV规范,其目的是为金融IC卡.金融终端.支付系统以 ...

  8. Quatre 2D的绘图功能的三个步骤(上下文,绘图,渲染)

    一.qurza2d是怎么将绘图信息和绘图的属性绘制到图形上下文中去的? 说明: 新建一个项目,自定义一个view类和storyboard关联后,重写该类中的drowrect方法. 画线的三个步骤: ( ...

  9. iOS蓝牙4.0

    iOS的蓝牙用到了  CoreBluetooth 框架 首先导入框架 #import <CoreBluetooth/CoreBluetooth.h> 我们需要一个管理者来管理蓝牙设备,CB ...

  10. Linux定时任务设定

    使用crontab 命令进行设定. 详情可参见:http://blog.csdn.net/xiyuan1999/article/details/8160977. 共有6项构成,前5项为时间:分 时 天 ...