標籤:style blog http io ar color 使用 sp on
一、理論
1、App的各種資料在WP哪裡的?
很好介紹了這個問題。有InstalltionFolder, knownFolder, SD Card...
2、一個App的資料儲存概覽
主要分兩大部分,InstallationFolder和App Data Folder
3、Windows.Storage.ApplicationData 和 Windows.Security.Credentials簡述
其中利用Windows.Storage.ApplicationData,我們可以獲得3種不同的wp中檔案夾和2種設定儲存字典,C#操作如下
Windows.Storage.StorageFolder roam = Windows.Storage.ApplicationData.Current.RoamingFolder; Windows.Storage.StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; Windows.Storage.StorageFolder temp = Windows.Storage.ApplicationData.Current.TemporaryFolder; Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
二、Local Folder 和 Local Setting
1、LocalSetting使用,C#代碼如下:
// Create a simple settinglocalSettings.Values["exampleSetting"] = "Hello Windows";// Read data from a simple settingObject value = localSettings.Values["exampleSetting"];if (value == null){ // No data}else{ // Access data in value}// Delete a simple settinglocalSettings.Values.Remove("exampleSetting");
2、LocalFolder使用,C#代碼如下:
private async void writeTextToLocalStorageFile(string filename, string text) { var fold = Windows.Storage.ApplicationData.Current.LocalFolder;//開啟檔案夾 StorageFile file = await fold.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);//建立個檔案 await FileIO.WriteTextAsync(file, text);//在檔案裡面寫內容 } private async Task<string> readTextFromLocalStorage(string filename) { var fold = Windows.Storage.ApplicationData.Current.LocalFolder;//開啟檔案夾 StorageFile file = await fold.GetFileAsync(filename);//開啟對應的檔案 string result = await FileIO.ReadTextAsync(file);//讀取檔案裡面的內容 return result; }
Note:這裡對檔案的操作是十分簡單的,步驟是在電腦管理檔案的步驟是類似的。
三、Roaming Setting 和Roaming Folder
如果使用者在多個裝置上安裝了你的程式,如果所有的裝置可以共用相同的設定資訊是很好的體驗。而Roaming data提供了一個應用程式可以在多個不同的物理裝置同步資料和設定方法。它以一個檔案夾和設定字典的形式自動儲存在使用者的OneDrive。Roaming data的大小限制於ApplicationData.RoamingStorageQuota(一般是100k左右,但它不會佔用你Onedrive的空間大小)。其同步過程圖如下:
Roaming Setting 和Roaming Folder的操作方式與上面的Local Folder 和 Local Setting操作方式一樣,在這不一一介紹。但是需要在不同裝置中監視Roaming data的改變事件。C#代碼如下:
Windows.Storage.ApplicationData.Current.DataChanged += Current_DataChanged;...void Current_DataChanged(ApplicationData sender, object args){ // Refresh your settings...}
資料的同步是發生在背景。
四、幾種不同方式擷取檔案的方式
wp8.1 Study10:APP資料存放區