我有一个显示来自驱动器 D: 的数据的网格视图:
代码:
ObservableCollection<Book> datasource = new ObservableCollection<Book>();
Book book = new Book();
book.Name = file.DisplayName.ToString().Replace("_", " ");
try
{
string thumbPath = @"D:\Kipin\thumb\komik";
StorageFolder komikthumb = await StorageFolder.GetFolderFromPathAsync(thumbPath);
thumbFile = await komikthumb.GetFileAsync(file.Name.ToString() + ".png");
string path1 = komikthumb.Path;
string filePath = Path.Combine(path1, file.Name.ToString() + ".png");
if (System.IO.File.Exists(filePath))
{
book.Image = thumbFile.Path;
}
catch
{}
}
datasource.Add(book);
this.komikGridView.ItemsSource = datasource;
Book.cs:
public class Book
{
public string Name { get; set; } }
public string Image { get; set; }
}
上面的代码没有成功在gridview中显示来自D的数据并收到错误消息:
Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll.
如何处理?
建议按照以下步骤打开文件权限并使用Windows.Storage命名空间下的File Api。
1.在Package.appxmanifest中添加受限的
broadFileSystemAccess
能力。
<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
2.在“设置”中打开文件权限。