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. Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  2. Unity3d 制作动态Mesh且可以随地面凹凸起伏

    适用情景:主角带着光环,光环用一张贴图,要贴在地面上,并且随地面凹凸起伏 //代码 using UnityEngine; using System.Collections; [RequireCompo ...

  3. ios 关于UIView 的multipleTouchEnabled 和 exclusiveTouch

    做项目时发现,在一个界面上的2个button竟然可以同时点击,依次push进去了2个 controller!我就产生了疑问,一个view的multipleTouchEnabled属性默认是false啊 ...

  4. delphi的取整函数round、trunc、ceil和floor

    delphi的取整函数round.trunc.ceil和floor 首先引入math单元 uses math; 1.Round(四舍六入五留双) 功能说明:对一个实数进行四舍五入.(按照银行家算法) ...

  5. glib-2.49.4 static build step in windows XP

    export LIBFFI_CFLAGS=" -I/usr/local/lib/libffi-3.2.1/include " \ export LIBFFI_LIBS=" ...

  6. ABAP 特殊透明表

    ADRC   地址 (业务地址服务)   存储所有的有关地址的记录 MSEG  凭证段:物料   存储物料的所有凭证(包括销售.交货.采购等) VBPA-VBELN(销售凭证)  VBPA-KUNNR ...

  7. [NSURLSession/Delegate]用Post方式获取网络数据并把数据显示到表格

    #pragma mark 实现NSURLSessionDataDelegate代理 @interface ViewController ()<UITableViewDataSource,UITa ...

  8. expression<Func<object,Bool>> 及 Func<oject,bool>用法

    using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using ...

  9. 【processing】小代码2

    函数: 绘制直线自由图形: beginShape(), vertex(), endShape() 分别是绘制图形开始,连接图形的节点,绘制结束 endShape(CLOSE)表示闭合图形. 绘制曲线边 ...

  10. php数据访问(修改)

    修改:跟添加相似,需要显示默认值 先嵌入php代码  查询数据库 $code = $_GET["c"]; $db = new MySQLi("localhost" ...