添加broadFileSystemAccess后UWP应用仍无法访问文件求助
问题
我正在开发一款UWP应用,需要访问用户通过FilePicker选择的文件。我了解UWP运行在沙箱环境中,默认不具备文件访问权限。经搜索得知需在App Manifest中添加broadFileSystemAccess权限,并在系统设置中开启应用的文件访问权限,但完成这些操作后应用仍无法访问文件。
我尝试移除所有其他权限,但问题仍未解决。
App Manifest代码
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10" xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp iot uap3 rescap"> <Identity Name="ce94a06e-3ef9-4040-997e-5ccd7ad2af52" Publisher="CN=Adhul" Version="1.0.0.0" /> <mp:PhoneIdentity PhoneProductId="ce94a06e-3ef9-4040-997e-5ccd7ad2af52" PhonePublisherId="00000000-0000-0000-0000-000000000000"/> <Properties> <DisplayName>Project V</DisplayName> <PublisherDisplayName>Adhul</PublisherDisplayName> <Logo>Assets\StoreLogo.png</Logo> </Properties> <Dependencies> <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" /> </Dependencies> <Resources> <Resource Language="x-generate"/> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="Project_V.App"> <uap:VisualElements DisplayName="Project V" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="Project V" BackgroundColor="transparent"> <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/> <uap:SplashScreen Image="Assets\SplashScreen.png" /> </uap:VisualElements> </Application> </Applications> <Capabilities> <rescap:Capability Name="broadFileSystemAccess"/> </Capabilities> </Package>
文件选择器代码
var file = new FileOpenPicker(); file.ViewMode = PickerViewMode.Thumbnail; file.FileTypeFilter.Add("*"); StorageFile virus = await file.PickSingleFileAsync(); if( virus != null) { using(var md5 = MD5.Create()) { try { using (var stream = File.OpenRead(virus.Path)) { var l = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", String.Empty).ToLower(); hash_t.Text = l; //find the corrseponding signature if (File.ReadLines("signatures.txt").Contains(l) != false) { this.text.Text = "Malware detected"; } else { this.text.Text = "Sounds Safe"; } } } catch (UnauthorizedAccessException) { this.text.Text = " Go to Settings > Privacy > File System > Allow access uwp apps "; } } } else { };
解决方案
核心问题:误用.NET传统IO类
你代码中使用的File.OpenRead、File.ReadLines是.NET Framework的传统文件IO方法,这类方法不兼容UWP的沙箱权限模型,即便添加了broadFileSystemAccess权限也无法正常工作。必须使用UWP专属的文件访问API才能适配沙箱环境。
具体修复步骤
读取用户选择的文件
替换File.OpenRead(virus.Path),改用StorageFile自带的异步方法获取流:using (var stream = await virus.OpenStreamForReadAsync()) { var l = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", String.Empty).ToLower(); hash_t.Text = l; // 后续逻辑保持不变 }读取本地签名文件
先确保signatures.txt的属性设置正确:在VS中选中文件,右键设置“生成操作”为内容,“复制到输出目录”为如果较新则复制。然后用UWP API读取:var signatureFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///signatures.txt")); var lines = await FileIO.ReadLinesAsync(signatureFile); if (lines.Contains(l)) { this.text.Text = "Malware detected"; } else { this.text.Text = "Sounds Safe"; }权限优化
通过FileOpenPicker选择的文件,UWP会自动授予该文件的访问权限,不需要添加broadFileSystemAccess权限。这个权限仅用于不通过文件选择器直接访问任意文件路径的场景,你的应用可以直接移除该权限,减少不必要的权限申请。
内容的提问来源于stack exchange,提问作者CaptainBeasst
相关产品推荐
相关产品推荐

