WindowsPhone8.1 应用数据操作
1、添加应用数据
ApplicationData.Current.LocalSettings是操作应用数据的一个类
这是获取本地应用数据的设置容器
ApplicationData.Current.LocalSettings
添加数据 dataSettings.Values["key"]="hello ";
删除数据 dataSetting.Values.Remove("key");
读取数据 datalocalSetting.Values["key"].ToString();
判断数据是否存在 dataSettings.Values.ContainsKey("key")
容器的创建
ApplicationDataContainer dataSettings=ApplicationData.Current.LocalSettings;
dataSettings.CreateContainer("example", ApplicationDataCreateDisposition.Always);
添加数据
dataSettings.Containers["example"].Values["h"] = "你好吗是不是";
读取数据
await new MessageDialog(datalocalSetting.Containers["example"].Values["h"].ToString()).ShowAsync();
删除容器
dataSetting.DeleteContainer("example");
2、应用文件操作
WP8.1中包括以下三种文件
ApplicationData.Current.TemporaryFolder; 临时文件
ApplicationData.Current.RoamingFolder; 漫游文件
ApplicationData.Current.LocalFolder; 本地文件
3、使用FileIO写入以及读取文件
var local = ApplicationData.Current.LocalFolder;
var fileFolder = await local.CreateFolderAsync("hello", CreationCollisionOption.ReplaceExisting);
IStorageFile file = await fileFolder.CreateFileAsync("data.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, "你好吗使用File读取");
读取文件
var local = ApplicationData.Current.LocalFolder;
StorageFolder fileFolder = await local.GetFolderAsync("hello");
var file = await fileFolder.GetFileAsync("data.txt");
string text = await FileIO.ReadTextAsync(file);
await new MessageDialog(text).ShowAsync();
使用(StreamWriter 以及(StreamReader 来读取数据
var local = ApplicationData.Current.LocalFolder;
var fileFolder = await local.CreateFolderAsync("hello", CreationCollisionOption.ReplaceExisting);
IStorageFile file = await fileFolder.CreateFileAsync("data.txt", CreationCollisionOption.ReplaceExisting);
// await FileIO.WriteTextAsync(file, "你好吗使用File读取");
var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (StreamWriter sr = new StreamWriter(stream.AsStreamForWrite()))
{
sr.Write("这是使用StreamReader来读取数据");
}
var local = ApplicationData.Current.LocalFolder;
StorageFolder fileFolder = await local.GetFolderAsync("hello");
var file = await fileFolder.GetFileAsync("data.txt");
var stream =await file.OpenReadAsync();
// string text = await FileIO.ReadTextAsync(file);
using (StreamReader sr=new StreamReader(stream.AsStreamForRead()))
{
string text = sr.ReadToEnd();
await new MessageDialog(text).ShowAsync();
}
使用第三种方法读取文件
var local = ApplicationData.Current.LocalFolder;
var fileFolder = await local.CreateFolderAsync("hello", CreationCollisionOption.OpenIfExists);
IStorageFile file = await fileFolder.CreateFileAsync("data.txt", CreationCollisionOption.ReplaceExisting);
byte[] filebyte = System.Text.Encoding.UTF8.GetBytes("hello worlf");
using (var s=await file.OpenStreamForWriteAsync())
{
s.Write(filebyte, 0, filebyte.Length);
}
读取文件
var local = ApplicationData.Current.LocalFolder;
StorageFolder fileFolder = await local.GetFolderAsync("hello");
var file = await fileFolder.GetFileAsync("data.txt");
using (var s=await file.OpenStreamForReadAsync())
{
byte[] bytes=new byte[1024];
int count = s.Read(bytes,0,(int)s.Length);
string text= Encoding.UTF8.GetString(bytes, 0, count);
await new MessageDialog(text).ShowAsync();
}
删除文件夹
var local = ApplicationData.Current.LocalFolder;
var localFolder = await local.GetFolderAsync("hello");
await localFolder.DeleteAsync();
通过Ibuffe来读取图片等二进制数据
StorageFolder local = ApplicationData.Current.LocalFolder;
var file=await local.CreateFileAsync("1.dat", CreationCollisionOption.ReplaceExisting);
using (InMemoryRandomAccessStream memoryStream=new InMemoryRandomAccessStream())
{
using (DataWriter dataWriter=new DataWriter(memoryStream))
{
dataWriter.WriteInt32(Encoding.UTF8.GetByteCount("你好"));
dataWriter.WriteString("你好");
var buffer = dataWriter.DetachBuffer();
await FileIO.WriteBufferAsync(file, buffer);
}
}
读取二进制数据
StorageFolder local = ApplicationData.Current.LocalFolder;
StorageFile file = await local.GetFileAsync("1.dat");
IBuffer buffer = await FileIO.ReadBufferAsync(file);
using (DataReader dataReader=DataReader.FromBuffer(buffer))
{
Int32 size = dataReader.ReadInt32();
string fileContent = dataReader.ReadString((uint)size);
await new MessageDialog(fileContent).ShowAsync();
}
读取项目文件下的文件 1.txt 是项目下建立的文件 凡是项目下的图片以及文件都可以这样访问
var storageFile=await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///1.txt"));
string text=await FileIO.ReadTextAsync(storageFile);
await new MessageDialog(text).ShowAsync();
遍历所编译后安装包下的文件
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
string txt="";
foreach (var file in await folder.GetFilesAsync())
{
txt = txt + file.Name+"\r\n";
}
foreach (var item in await folder.GetFoldersAsync())
{
txt = txt + item.Name+ "\r\n";
foreach (var file in await item.GetFilesAsync())
{
txt = txt + file.Name+"\r\n";
}
}
await new MessageDialog(txt).ShowAsync();