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
}
wp8 入门到精通 Utilities类 本地存储+异步的更多相关文章
- 微信小程序入门一: 简易form、本地存储
实例内容 登陆界面 处理登陆表单数据 处理登陆表单数据(异步) 清除本地数据 实例一: 登陆界面 在app.json中添加登陆页面pages/login/login,并设置为入口. 保存后,自动生成相 ...
- wp8 入门到精通 虚拟标示符 设备ID
//获得设备虚拟标示符 wp8 public string GetWindowsLiveAnonymousID() { object anid = new object(); string anony ...
- wp8 入门到精通
<StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal" ...
- wp8 入门到精通 仿美拍评论黑白列表思路
static bool isbool = false; private void BindGameDelete() { Tile tile = new Tile(); List<Color> ...
- wp8 入门到精通 生命周期
- wp8 入门到精通 定时更新瓷贴
public class ScheduledAgent : ScheduledTaskAgent { static ScheduledAgent() { Deployment.Current.Disp ...
- wp8 入门到精通 ImageCompress 图片压缩
//实例化选择器 PhotoChooserTask photoChooserTask = new PhotoChooserTask(); BitmapImage bimg; int newPixelW ...
- wp8 入门到精通 Gallery
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.Resources> ...
- wp8 入门到精通 MultiMsgPrompt
List<NotifyMsg> arraymsg = new List<NotifyMsg>(); List<NotifyInfo> ArrayNotifyInfo ...
随机推荐
- SQL Sever2008r2 数据库服务各种无法启动的解决办法
一.Sql Server服务远程过程调用失败解决 以前出现过这个问题,那时候是因为把实例安装在了D盘,后来D盘被格式化了.然后,这些就没了.今天早上打开电脑,竟然又出现这个问题,可是Server200 ...
- nginx 下 bootstrap fa 字体异常问题
server { listen 8082; # server_name 192.168.16.88; # root /home/ywt/workspace/kuF/web/statics; # aut ...
- 8.eclipse调试smali
一.重打开包APK 1.apktool解包文件 apktool d -d XXX.apk 这里注意使用-d参数,生成的smali文件才是以java结尾的,才能被eclipse识别 2.找到Androi ...
- Intel项目所用jquery小知识点总结
1.$("#tdGeo input[type='checkbox']:checked") ---筛选出所有已经Check的Checkbox 2.$("#tdCount ...
- 抓取网页内容生成kindle电子书
参考: http://calibre-ebook.com/download_linux http://blog.codinglabs.org/articles/convert-html-to-kind ...
- 基于隐马尔科夫模型(HMM)的地图匹配(Map-Matching)算法
文章目录 1. 1. 摘要 2. 2. Map-Matching(MM)问题 3. 3. 隐马尔科夫模型(HMM) 3.1. 3.1. HMM简述 3.2. 3.2. 基于HMM的Map-Matchi ...
- iOS 目录的使用
Table 1-1 Commonly used directories of an iOS app Directory Description AppName.app This is the ap ...
- android课件和源代码
ppt.rar 代码.rar 也可以自己从这里下载http://pan.baidu.com/share/link?shareid=1287391506&uk=2634355140
- ABAP 行列稳定刷新语句
DATA stbl TYPE lvc_s_stbl. stbl-row = 'X'." 基于行的稳定刷新 stbl-col = 'X'." 基于列稳定刷新 ...
- 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)
1.问题复现 spring 3.0 + hibernate 3.2 spring mvc使用注解方式:service使用@service注解 事务使用@Transactional 事务配置使用 < ...