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