首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows 8 Store Apps 開發實戰 (24):Application Data 與 Package 檔案操作全攻略

     編輯:關於.NET

前言:資料儲存的三大領域

Windows 8 App 沙箱機制下,檔案存取主要分三大區塊:Application Data(應用程式資料)、Package(安裝包)、Removable Devices(可移除裝置)。本篇系統整理各區域的存取 API、路徑對應、漫游機制與實戰技巧。

Application Data - 應用程式專屬儲存空間

三大資料夾對照表

屬性用途實體路徑備註
LocalFolder本機永久資料%USERPROFILE%\AppData\Local\Packages\{PackageId}\LocalState不隨裝置同步
RoamingFolder漫游資料(跨裝置同步)%USERPROFILE%\AppData\Local\Packages\{PackageId}\RoamingState受 RoamingStorageQuota 限制
TemporaryFolder暫存資料%USERPROFILE%\AppData\Local\Packages\{PackageId}\TempState系統可隨時清理

檔案讀寫實戰

// 寫入漫游資料夾
StorageFile fileWrite = await _roamingFolder.CreateFileAsync(
    @"webabcdTest\readWriteDemo.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(fileWrite, "I am webabcd: " + DateTime.Now);

// 透過 ms-appdata:// 協定讀取(支援相對路徑)
StorageFile fileRead = await StorageFile.GetFileFromApplicationUriAsync(
    new Uri("ms-appdata:///roaming/webabcdTest/readWriteDemo.txt"));
string text = await FileIO.ReadTextAsync(fileRead);

設定值 - 輕量級 Key-Value 儲存

LocalSettings、RoamingSettings 提供字典式儲存,支援巢狀容器與複合值:

// 簡單讀寫
var localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["key"] = "I am webabcd";
lblMsg.Text = (string)localSettings.Values["key"];

// 分組容器
var container = localSettings.CreateContainer("groupName", ApplicationDataCreateDisposition.Always);
container.Values["key"] = "grouped value";

// 複合值(父子關係)
var composite = new ApplicationDataCompositeValue();
composite["child1"] = "abc";
composite["child2"] = "xyz";
localSettings.Values["parent1"] = composite;

// 高優先度同步:Key 為 "HighPriority" 時系統以最快速度跨裝置同步
localSettings.Values["HighPriority"] = "urgent data";

版本控制與資料遷移

// 設定版本號並提供遷移邏輯
await ApplicationData.Current.SetVersionAsync(1, new ApplicationDataSetVersionHandler(SetVersionHandler));

async void SetVersionHandler(SetVersionRequest request)
{
    var deferral = request.GetDeferral();
    // 依 CurrentVersion 與 DesiredVersion 執行資料轉換
    deferral.Complete();
}

ms-appdata:// 協定引用媒體檔

XAML 直接綁定:

<Image Source="ms-appdata:///temp/Logo.png" />
<Image Source="ms-appdata:///local/photos/avatar.jpg" />
<Image Source="ms-appdata:///roaming/settings.xml" />

對應關係:LocalFolder → ms-appdata:///local/、RoamingFolder → ms-appdata:///roaming/、TemporaryFolder → ms-appdata:///temp/。

Package 安裝包內檔案 - 唯讀資源

安裝包內檔案唯讀,適合放置預設資源、設定檔、樣板等:

// 透過 ms-appx:// 協定讀取
StorageFile fileRead = await StorageFile.GetFileFromApplicationUriAsync(
    new Uri("ms-appx:///FileSystem/PackageData/readWriteDemo.txt"));
string text = await FileIO.ReadTextAsync(fileRead);

// 也可用 Package.Current.InstalledLocation
StorageFile fileRead2 = await Package.Current.InstalledLocation.GetFileAsync(
    @"FileSystem\PackageData\readWriteDemo.txt");

可移除儲存裝置 - USB、SD 卡存取

需在 Manifest 勾選 removableStorage Capability,並宣告「檔案類型關聯」支援的副檔名。

// 列舉所有可移除裝置根目錄
StorageFolder removableDevices = KnownFolders.RemovableDevices;
IReadOnlyList<StorageFolder> drives = await removableDevices.GetFoldersAsync();
foreach (var drive in drives)
{
    // 僅能存取已宣告副檔名的檔案
    IReadOnlyList<StorageFile> files = await drive.GetFilesAsync();
}

// 透過 DeviceInformation 取得特定裝置
var interfaces = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector());
if (interfaces.Count > 0)
{
    StorageFolder drive = StorageDevice.FromId(interfaces[0].Id);
    // 讀寫操作...
}

完整程式碼下載

原始碼完整包(含所有範例專案)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved