win8 app 应用程序数据存储
Win8应用程序数据存储http://msdn.microsoft.com/library/windows/apps/Hh464917
如果在调试的时候想要查看当前的数据或文件是否保存成功可以在PC 里面可以查看:
地址为(因系统安装位置不同可能会有出入,这个是我自己的电脑查看的位置): C:\Users\你的用户名\AppData\Local\Packages\...
AppData在Metro App中的存储主要由两种形式,一种是键值对的形式,还有一种是StorageFile文件的存储形式。
//获取应用程序设置的本地数据存储容器
ApplicationDataContainer localSettings =ApplicationData.Current.LocalSettings;
//最简单的键值对存储
localSettings.Values["key"] = "内容...";
if (localSettings.Values.ContainsKey("key"))
{
this.txtBox.Text = localSettings.Values["key"].ToString();
}
//删除数据
private void btnRemove_Click(object sender, RoutedEventArgs e)
{
localSettings.Values.Remove("key");
object obj = localSettings.Values["key"];
if (obj == null)
{
this.txtBox.Text = "空的";
}
}
//获取应用程序设置的本地数据存储容器
ApplicationDataContainer localSettings =ApplicationData.Current.LocalSettings;
// 复合值存储
ApplicationDataCompositeValue compositeValue = new ApplicationDataCompositeValue();
compositeValue["A"] = "Hello";
compositeValue["B"] = "World";
localSettings.Values["key"] = compositeValue;
if (localSettings.Values.ContainsKey("key"))
{
ApplicationDataCompositeValue compositeValue = localSettings.Values["key"] as ApplicationDataCompositeValue;
this.txtBox.Text = compositeValue["A"].ToString() + "~" + compositeValue["B"].ToString();
}
localSettings.Values.Remove("key");
ApplicationDataCompositeValue compositeValue = localSettings.Values["key"] as ApplicationDataCompositeValue;
if (compositeValue == null)
{
this.txtBox.Text = "空的";
}
//获取应用程序设置的漫游数据存储容器
注: ApplicationDataContainer roamingSettings =ApplicationData.Current.RoamingSettings;
你有两台电脑,都安装了Win 8 , 同时他们都安装了一款应用, 那么只要你用同一个Live 帐号登陆这两台电脑,那么这个应用的RoamingSettings 就会被同步。在两台电脑中是一样的。
LocalSettings :数据仅对本地App有效,不参与同步。
ApplicationDataContainerlocalSettings = ApplicationData.Current.LocalSettings;
//创建一个mtxx 的数据存储容器(在当前设置的容器中创建一个特别指定的数据存储容器)
ApplicationDataContainer container = localSettings.CreateContainer("mtxx", ApplicationDataCreateDisposition.Always);
ApplicationDataCompositeValue compositeValue=new ApplicationDataCompositeValue();
compositeValue["A"] = "Hello";
if (localSettings.Containers.ContainsKey("mtxx"))
{
localSettings.Containers["mtxx"].Values["key"] = compositeValue;
}
if (localSettings.Containers["mtxx"].Values.ContainsKey("key"))
{
ApplicationDataCompositeValue compositeValue = localSettings.Containers["mtxx"].Values["key"] as ApplicationDataCompositeValue;
if (compositeValue.ContainsKey("A"))
{
this.txtBox.Text = compositeValue["A"].ToString();
}
}
删除数据 删除容器
if (localSettings.Containers.ContainsKey("mtxx"))
{
localSettings.Containers["mtxx"].Values.Remove("key");
}
ApplicationDataCompositeValue compositeValue = localSettings.Containers["mtxx"].Values["key"] as ApplicationDataCompositeValue;
if (compositeValue == null)
{
this.txtBox.Text = "空的";
}
localSettings.DeleteContainer("mtxx");
if (!localSettings.Containers.ContainsKey("mtxx"))
{
MessageDialog messageDialog = new MessageDialog("已删除容器");
await messageDialog.ShowAsync();
}
StorageFile的存储,以文件的形式进行存储存入数据
StorageFolder folder=ApplicationData.Current.LocalFolder;
创建本地存储txt文件
StorageFile file = await folder.CreateFileAsync("mtxx.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, System.DateTime.Now.ToString());
读取本地txt文件
try
{
StorageFile file = await folder.GetFileAsync("mtxx.txt");
if (file != null)
{
this.txtBox.Text = await FileIO.ReadTextAsync(file);
}
}
catch(Exception ex)
{
MessageDialog message = new MessageDialog(ex.Message);
message.ShowAsync();
}
删除txt 文件
try
{
StorageFile file = await folder.GetFileAsync("mtxx.txt");
if (file != null)
{
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
}
catch (Exception ex)
{
MessageDialog message = new MessageDialog(ex.Message);
message.ShowAsync();
}
static async public Task SaveDataToLocalAsync(T data,string fileName)
{
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (IOutputStream outStream = raStream.GetOutputStreamAt(0))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(outStream.AsStreamForWrite(), data);
await outStream.FlushAsync();
}
}
static async public Task GetDataFromLocalAsync(string filename)
{
T sessionState_ = default(T);
try
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
if (file == null) return sessionState_;
IInputStream inStream = await file.OpenSequentialReadAsync();
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
sessionState_ = (T)serializer.ReadObject(inStream.AsStreamForRead());
}
catch (Exception)
{
}
return sessionState_;
}
async private void btnSave_Click(object sender, RoutedEventArgs e)
{
List stuList = new List() { new Student{ ID="1",Name="A"},
new Student{ ID="2",Name="B"},
new Student{ ID="3",Name="C"},
new Student{ ID="4",Name="D"}};
await SaveDataToLocalAsync(stuList, "mtxx.txt");
}
async private void btnDisplay_Click(object sender, RoutedEventArgs e)
{
List stuList = await GetDataFromLocalAsync("mtxx.txt");
MessageDialog message = new MessageDialog(stuList.Count.ToString());
await message.ShowAsync();
}
页:
[1]