Windows 8 App 沙箱機制下,檔案存取主要分三大區塊:Application Data(應用程式資料)、Package(安裝包)、Removable Devices(可移除裝置)。本篇系統整理各區域的存取 API、路徑對應、漫游機制與實戰技巧。
| 屬性 | 用途 | 實體路徑 | 備註 |
|---|---|---|---|
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);
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();
}
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/。
安裝包內檔案唯讀,適合放置預設資源、設定檔、樣板等:
// 透過 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");
需在 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);
// 讀寫操作...
}