Windows Phone 8 的文件和存储

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

WP8的文件和存储

内容预告:

∙特殊的文件夹(Shared/Media,Shared/ShellContent,Shared/Transfer)

∙用ISET浏览本地文件夹

∙后台文件传输

∙使用SD存储卡

但不包括:

∙本地数据库(基于LINQ的sqlce)

∙SQLite

本地数据存储概览:打包管理器把所有的App放到"安装文件夹",App存储数据到"本地文件夹"。

定位存储位置的不同方式:

WP8文件存储的备选方案:三种方式

// WP7.1 IsolatedStorage APIs

varisf = IsolatedStorageFile.GetUserStoreForApplication();

IsolatedStorageFileStreamfs = new

IsolatedStorageFileStream("CaptainsLog.store", FileMode.Open, isf)); ...

// WP8 Storage APIs using URI

StorageFilestorageFile = await

Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new

Uri("ms-appdata:///local/CaptainsLog.store "));

...

// WP8 Storage APIs

Windows.Storage.StorageFolderlocalFolder =

Windows.Storage.ApplicationData.Current.LocalFolder;

Windows.Storage.StorageFilestorageFile = await

localFolder.GetFileAsync("CaptainsLog.store");

用WP7.1的方式:

IsolatedStorage类在System.IO.IsolatedStorage命名空间里:

∙IsolatedStorage,在独立存储区表达文件和文件夹

∙IsolatedFileStream,在IsolatedStorage中暴露一个文件流到文件存储。

∙IsolatedStorageSettings,以键值对(Dictionary)方式存储。

保存数据:

privatevoid saveGameToIsolatedStorage(string message)

{

using (IsolatedStorageFileisf =

IsolatedStorageFile.GetUserStoreForApplication())

{

using (IsolatedStorageFileStreamrawStream = isf.CreateFile("MyFile.store")) {

StreamWriter writer = new StreamWriter(rawStream);

writer.WriteLine(message); // save the message

writer.Close();

}

}

}

读取数据:

privatevoid saveGameToIsolatedStorage(string message)

{

using (IsolatedStorageFileisf =

IsolatedStorageFile.GetUserStoreForApplication())

{

using (IsolatedStorageFileStreamrawStream = isf.CreateFile("MyFile.store")) {

StreamWriter writer = new StreamWriter(rawStream);

writer.WriteLine(message); // save the message

writer.Close();

}

}

}

保存键值对数据:记着在最后调用Save函数保存。

void saveString(string message, string name)

{

IsolatedStorageSettings.ApplicationSettings[name] =message;

IsolatedStorageSettings.ApplicationSettings.Save();

}

读取键值对数据:记着要先检查是否有这个键,否则要抛异常。

string loadString(string name)

{

if (IsolatedStorageSettings.ApplicationSettings.Contains(name))

{

return (string)

IsolatedStorageSettings.ApplicationSettings[name];

}

else

returnnull;

}

WinPRT的存储方式:在Windows.Storage命名空间下:

∙StorageFolder

∙StorageFile

∙不支持ApplicationData.LocalSettings,只能用IsolatedStorageSettings或自定义文件

用StorageFolder保存数据:

privateasyncvoid saveGameToIsolatedStorage(string message)

{

// Get a reference to the Local Folder

Windows.Storage.StorageFolderlocalFolder

=Windows.Storage.ApplicationData.Current.LocalFolder;

// Create the file in the local folder, or if it already exists, just open it

Windows.Storage.StorageFilestorageFile =

await localFolder.CreateFileAsync("Myfile.store",

CreationCollisionOption.OpenIfExists);

Stream writeStream= await storageFile.OpenStreamForWriteAsync();

using (StreamWriter writer = new StreamWriter(writeStream))

{

await writer.WriteAsync(logData);

}

}

读取数据:

privateasyncvoid saveGameToIsolatedStorage(string message)

{

// Get a reference to the Local Folder

Windows.Storage.StorageFolderlocalFolder

=Windows.Storage.ApplicationData.Current.LocalFolder;

// Create the file in the local folder, or if it already exists, just open it

相关文档
最新文档