文件访问权限
通用Windows应用程序(应用程序)默认可以访问某些文件系统位置。 应用程序还可以通过文件选择器或通过声明权限访问其他位置
直接访问规定位置的文件(受限)
Application install directory(即你的App所在的文件夹)
- 通过StorageFolder
- 通过Uri (返回StorageFile)
用户数据位置(即你的App存储数据的文件夹。其中包括:local,roaming,temporary。这些文件夹在app 安装时自动创建)
- ApplicationData.Current.LocalFolder (返回StorageFolder对象)
- 对于RoamingFolder 和 TemporaryFolder,以此类推,代替LocalFolder即可
- 同样可以用Uri(返回StorageFile)
无限制访问任何位置的文件
通过FileOpenPicker 或 FolderPicker
这个方法可以访问用户任何地址的文件,返回一个StorageFile或StorageFolder。
注意,假如利用Windows.Storage.StorageFile就无法简单地通过路径获取文件。
详细请见
通过在package manifest中设定权限(不推荐)
StorageFile和StorageFolder的一些操作
获取文件
- File access using Windows.Storage API via URIs
var file = await Windows.StorageFile.GetFileFromApplicationUriAysnc(new Uri("ms-appdata:///local/AppConfig.xml"))
- File access using Windows.Storage API via StorageFolder references
var localFolder = windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile storageFile = await localFolder.GetFileAsync("CaptainsLog.store");
write text to file
private async void writeTextToLocalStorageFile(string filename, string text){
StorageFolder fold = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await fold.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, text);}
read from file to a string
private async Task<string> readTextFromLocalStorage(string filename){
var fold = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await fold.GetFileAsync(filename);
return result;
}
KnownFolders Class
提供某些具有特定用户内容文件夹(如音乐,图片等等)
常被用来设置在FileOpenPicker的SuggestedStartLocation 属性中,即为打开picker时首先显示的文件夹,包括PicturesLibrary等等
AccessCache
用处:store references to files and folders and their permissions so that the user can reopen them with one tap
注意:这个API不仅存储文件和文件夹,还存储他们的Access Permissions。所以能够解决存储了文件路径却打不开文件的问题
FutureAccessList
FutureAccessList is a list you can use to store files and/or locations (like folders) and easily access them in the future
能存储1000个item,但不会自动管理,满了之后必须手动剔除
MostRecentlyUsedList(MRU)
The MRU is a list you can use to track the files and folders your user accesses frequently
只能存储25个item,当超出范围会自动剔除旧item
存
// Open the picker for the user to pick a file
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null) {
// Save the picked file in the AccessCache
// Add to MRU with metadata (For example, a string that represents the date)
string mruToken = StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "20120716");
// Add to FA without metadata
string faToken = StorageApplicationPermissions.FutureAccessList.Add(file);
}
else
{
// The file picker was dismissed with no file selected to save
}
取
// get the token for the first item in our MRU
// use it to retrieve a StorageFile for that file
String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First().Toke;
StorageFile retrievedFile = await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(mruFirstToken);...
// Retrieve tokens for all items in the MRU
AccessListEntryView mruEntries = StorageApplicationPermissions.MostRecentlyUsedList.Entries;
if (mruEntries.Count > 0)
{
foreach (AccessListEntry entry in mruEntries)
{
String mruToken = entry.Token;
// Continue processing the MRU entry
...
}
}