You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GridView读取D盘数据报UnauthorizedAccessException错误的解决方法

解决UWP访问D盘文件的UnauthorizedAccessException问题

核心原因

UWP应用的沙箱机制默认限制对本地文件系统的直接访问,即便添加了普通文件关联声明,也无法直接通过路径访问D盘的任意文件夹/文件,必须遵循UWP的权限规则。

具体解决方案

1. 启用broadFileSystemAccess受限权限(适用于需直接访问指定路径的场景)

在Package.appxmanifest中添加权限声明:

<Package
    ...
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="uap mp rescap">
    ...
    <Capabilities>
      <rescap:Capability Name="broadFileSystemAccess" />
    </Capabilities>
</Package>

注意:该权限属于受限功能,提交微软商店需额外审核,且用户需手动在系统设置开启权限(设置→隐私和安全性→文件系统→找到你的应用并启用)。

2. 改用文件选择器让用户主动授权(推荐,符合UWP设计规范)

UWP推荐通过用户主动交互获取文件/文件夹访问权限,无需特殊权限,适配沙箱机制:

var folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
var selectedFolder = await folderPicker.PickSingleFolderAsync();

if (selectedFolder != null)
{
    // 保存权限,后续可直接访问该文件夹内容
    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", selectedFolder);
    
    var files = await selectedFolder.GetFilesAsync();
    ObservableCollection<Book> datasource = new ObservableCollection<Book>();
    foreach (var file in files)
    {
        Book book = new Book();
        book.Name = file.DisplayName.Replace("_", " ");
        
        try
        {
            var thumbFolder = await selectedFolder.GetFolderAsync("thumb\\komik");
            var thumbFile = await thumbFolder.TryGetItemAsync(file.Name + ".png") as StorageFile;
            if (thumbFile != null)
            {
                book.Image = thumbFile.Path;
            }
        }
        catch (Exception ex)
        {
            // 处理异常逻辑
        }
        datasource.Add(book);
    }
    komikGridView.ItemsSource = datasource;
}

3. 修正现有代码的错误点

  • 避免混用System.IO.File与UWP的StorageFile:沙箱中System.IO.File.Exists无法正确判断权限内文件,改用StorageFolder.TryGetItemAsync检查文件存在性。
  • 修复语法错误(原代码try/catch块闭合异常):
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);
    var thumbFile = await komikthumb.TryGetItemAsync(file.Name + ".png") as StorageFile;
    if (thumbFile != null)
    {
        book.Image = thumbFile.Path;
    }
}
catch
{}
datasource.Add(book);
this.komikGridView.ItemsSource = datasource;

关键注意事项

  • UWP仅能访问以下位置的文件:应用安装目录、应用数据目录、用户通过选择器授权的文件/文件夹、声明的系统库(如图片/文档库)、broadFileSystemAccess授权的位置。
  • 若绑定Image控件,建议将StorageFile转为BitmapImage而非直接用路径,避免权限问题:
if (thumbFile != null)
{
    using (var stream = await thumbFile.OpenAsync(FileAccessMode.Read))
    {
        BitmapImage bitmap = new BitmapImage();
        await bitmap.SetSourceAsync(stream);
        book.ImageBitmap = bitmap; // 在Book类中新增BitmapImage类型属性
    }
}

内容的提问来源于stack exchange,提问作者cielo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:02:35