WinRT文档阅读笔记,关于ApplicationData。

ApplicationData类提供到应用数据存储的访问。应用数据包括文件以及设置,并且分为三类:local(本地)、roaming(漫游)、temporary(临时)。

App可以访问的目录包括:

  • LocalFolder,存储在本地,备份到云端,应用更新过程中不受影响
  • LocalCacheFolder,存储在本地,但是不备份,应用更新过程中不受影响
  • RoamingFolder,在用户的所有安装了此App的设备上皆可访问
  • SharedLocalFolder,在App的不同用户之间共享,可能需要一定的访问权限
  • TemporaryFolder,临时用,系统可能将其删除

可以通过SetVersionAsync/Version来按应用的版本来分别存储数据。

获取LocalFolder:

StorageFolder m_localFolder{ Windows::Storage::ApplicationData::Current().LocalFolder() };

如果要获取RoamingFolder,则调用Windows::Storage::ApplicationData::Current().RoamingFolder()

创建一个文件:

    StorageFile sampleFile{ co_await m_localFolder.CreateFileAsync(L"dataFile.txt", CreationCollisionOption::ReplaceExisting) };

获取一个文件:

    winrt::hstring timestamp{ co_await Windows::Storage::FileIO::ReadTextAsync(file) };

从ApplicationData.Current可以获取当前App的实例。

如果需要访问App包里面的文件,需要使用Windows.ApplicationModel.Package.InstalledLocation.

Store and retrieve settings and other app data

App数据随着App的卸载会被清空,使用的时候需要特别注意。App数据有两大类:偏好设置和一般文件。

偏好设置用来保存App的设置和状态,保存的是有类型的数据:

  • WinRT的整型值
  • Boolean
  • Char16, String
  • DateTime, TimeSpan
  • GUID, Point, Size, Rect
  • ApplicationDataCompositeValue

以上除了ApplicationDataCompositeValue之外,都是是MIDL3.0中定义的类型

对于一般文件的话,Windows不会在意里面的存储内容。

获取LocalSetting和LocalFolder:

Windows.Storage.ApplicationDataContainer localSettings = 
   Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = 
   Windows.Storage.ApplicationData.Current.LocalFolder;

ApplicationDataCompositeValue示例:

// Composite setting

Windows.Storage.ApplicationDataCompositeValue composite = 
    new Windows.Storage.ApplicationDataCompositeValue();
composite["intVal"] = 1;
composite["strVal"] = "string";

localSettings.Values["exampleCompositeSetting"] = composite;

写入数据:

async void WriteTimestamp()
{
   Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = 
       new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");

   StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt", 
       CreationCollisionOption.ReplaceExisting);
   await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTimeOffset.Now));
}

读取数据可以使用:

  • Windows.Storage.StorageFolder.GetFileAsync
  • Windows.Storage.StorageFile.GetFileFromApplicationUriAsync
  • Windows.Storage.FileIO.ReadTextAsync.

对于漫游数据的使用,可是有配额的。可以通过ApplicationData.RoamingStorageQuota来查询配额。一些建议的漫游数据使用场景:

  • 保存用户首选项配置
  • 保存中间数据,让用户在不同设备上可以无缝切换
  • 需要通过侦听DataChanged事件来处理新进数据
  • 高优先级的漫游数据,可以配合RoamingSettings使用HighPriority
  • 不要漫游太多数据,不要指望漫游是即时性的

ApplicationData.RoamingStorageQuota返回的数据是以KB计算的

漫游数据需要登录微软账户。用户或者域管理员可以随时关停数据漫游。这样漫游数据智能在本地访问。保存在PasswordVault中的数据只会被同步到可信设备上。

漫游数据如果发生冲突,系统会选择最新的条目使其生效。如果条目是组合类型,那么冲突解决粒度是在整个组合,而不会考虑组合内部。

系统会仲裁资源的使用,如果漫游过度,可能会被暂停使用一段时间。

你可以为应用数据设置一个版本。通常版本号的选择是从一开始持续增长的。复用低版本号的数据可能带来数据丢失或者错乱的风险。漫游只会发生在同一版本的数据。

Temporary app data讲述如何使用临时数据。

应用数据的组织是通过ApplicationDataContainer对象来模拟目录。此对象可以最多实现32级嵌套。下面是个例子:

Windows.Storage.ApplicationDataContainer localSettings = 
    Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = 
    Windows.Storage.ApplicationData.Current.LocalFolder;

// Setting in a container
Windows.Storage.ApplicationDataContainer container = 
   localSettings.CreateContainer("exampleContainer", Windows.Storage.ApplicationDataCreateDisposition.Always);

if (localSettings.Containers.ContainsKey("exampleContainer"))
{
   localSettings.Containers["exampleContainer"].Values["exampleSetting"] = "Hello Windows";
}

删除一个容器,使用ApplicationDataContainer.DeleteContainer:

Windows.Storage.ApplicationDataContainer localSettings = 
    Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = 
    Windows.Storage.ApplicationData.Current.LocalFolder;

// Delete container

localSettings.DeleteContainer("exampleContainer");

其他

What Is the AppData Folder in Windows?

The LocalLow folder is the same as the Local folder, but is designed for “low integrity” applications that run with more restricted security settings. For example, Internet Explorer when run in Protected Mode only has access to the LocalLow folder. The difference doesn’t really matter for your personal use, but some applications just need a folder to write to because they don’t have access to the main Local folder.

(完)