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 ...
随机推荐
- python gui之tkinter界面设计pythonic设计
ui的设计,控件id的记录是一件比较繁琐的事情. 此外,赋值和读取数据也比较繁琐,非常不pythonic. 有没有神马办法优雅一点呢?life is short. 鉴于控件有name属性,通过dir( ...
- Visual Studio 2010添加新项缺失[ADO.NET 实体数据模型]解决方法
当进行ASP.NET MVC项目开发,准备使用EF进行数据库访问,我的开发模式是"Table First".于是,准备在Model目录新建EF的数据表映射文件.可是,在添加新项目窗 ...
- Database、User、Schema、Tables、Col、Row
可以把Database看作是一个大仓库,仓库分了很多很多的房间,Schema就是其中的房间,一个Schema代表一个房间,Table可以看作是每个Schema中的床,Table(床)就被放入每个房间中 ...
- Docker configure http proxy
from: http://stackoverflow.com/questions/23111631/cannot-download-docker-images-behind-a-proxy That' ...
- 【Unity3D】Invoke,InvokeRepeating ,Coroutine 延迟调用,周期性调用
Invoke和InvokeRepeating方法,可以实现延迟调用,和周期调用 第一个是执行一次,第二个是重复执行 void Invoke(string methodName, float time) ...
- CEF3开发者系列之进程和线程
CEF3是一个多进程架构框架,如果有了解过chromium的进程架构的,那么就很容易了解CEF3的多进程了.打开CEF3源代码中发布的cefclient实例,如果打开的页面带有flash或者其他插件. ...
- Apache OFBiz 研究记录01
作为Apache 的顶级项目: Apache OFBiz,功能十分强大,一般开发者很难用到全部功能. 这次笔者的研究主要集中在电子商务平台这一块,一步一步解构. OFBiz下载地址:http://of ...
- winform,wpf,winrt获取屏幕分辨率
winform 当前的屏幕除任务栏外的工作域大小 this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Widt ...
- mysql 删除重复数据保留只保留一条
SELECT * FROM (SELECT addTime FROM motorcade.car_msg_info GROUP BY addTime HAVING COUNT(addTime) > ...
- linux 卸载软件
sudo apt-get autoremove --purge 后跟要卸载的软件名称, --purge表示要完全卸载.