WinRT的存储对象模型

IStorageItem接口是核心。

IStorageFolder继承自IStorageItem,用于操作文件夹。

IStorageFile继承自IStorageItem,用于操作文件。

IStorageItemProperties定义的成员暴露存储项的属性,如缩略图,名称,类型等。

IStorageFolderQueryOperations用于搜索文件和文件夹。


StorageFolder类实现IStorageFolder和IStorageItemProperties。可能表示一个实际的文件夹,也可能表示虚拟的文件夹。

StorageFile类实现IStorageFile和IStorageItemProperties。表示一个实际的文件。

只有StorageFolder实现了IStorageFolderQueryOperations。


安装后,包的内容会被解压到%ProgramFiles%\WindowsApps\PackageFullName,此文件夹是只读的。

下面是读取的代码:

// Get StorageFolder object that represents our package's install directory:
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// In our package's directory go to the Assets subdirectory and find the Image.png file:
StorageFile file = await folder.GetFileAsync(@"Assets\Image.png");
或者通过URI:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Assets/Image.png"));

安装后系统创建的文件夹%UserProfile%\AppData\Local\Packages\PackageFamilyName 是可读写的,里面有3个子目录LocalState, RoamingState, 和TempState。

也可以通过对象或URI访问:

StorageFolder folder;
StorageFile file;
// Create and then access a local package file:
folder = ApplicationData.Current.LocalFolder;
file = await folder.CreateFileAsync("LocalFile.txt",
CreationCollisionOption.ReplaceExisting);
file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appdata:///local/LocalFile.txt"));
// Create and then access a roaming package file:
folder = ApplicationData.Current.RoamingFolder;
file = await folder.CreateFileAsync("RoamingFile.txt",
CreationCollisionOption.GenerateUniqueName);
file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appdata:///roaming/RoamingFile.txt"));
// Create and then access a temporary package file:
folder = ApplicationData.Current.TemporaryFolder;
file = await folder.CreateFileAsync("TemporaryFile.txt",
CreationCollisionOption.OpenIfExists);
file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appdata:///temp/TemporaryFile.txt"));
// The line below deletes all read-write package files:
await ApplicationData.Current.ClearAsync();

如果自己建一个文件夹命名为Indexed,Windows会自动帮助建索引。可以用下面的代码:

// Create the local package folder's "Indexed" subdirectory:
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFolder indexedSubdir = await localFolder.CreateFolderAsync("Indexed");
// Create two text files in the "Indexed" subdirectory:
await FileIO.WriteTextAsync(await indexedSubdir.CreateFileAsync("File1.txt"), "abc");
await FileIO.WriteTextAsync(await indexedSubdir.CreateFileAsync("File2.txt"), "abcd");
// Create a query that looks for .txt files containing "abcd"
var qo = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".txt" }) {
ApplicationSearchFilter = "abcd",
IndexerOption = IndexerOption.OnlyUseIndexer, // Indexed files only!
FolderDepth = FolderDepth.Deep // Search subdirectories too
};
StorageFileQueryResult searchResults = localFolder.CreateFileQueryWithOptions(qo);
// Perform the query and get the results
IReadOnlyList<StorageFile> result = await searchResults.GetFilesAsync();
// 'result' contains a single StorageFile object referring to File2.txt

通过Picker打开文件:

var fop = new FolderPicker { FileTypeFilter = {"*"} };
var folder = await fop.PickSingleFolderAsync();
if (folder == null) return; // User canceled the picker
// folder refers to the user-selected StorageFolder object

有3个Picker:FolderPicker, FileOpenPicker, 和FileSavePicker

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值