简体   繁体   English

应用程序第二次运行时拒绝访问文件

[英]Access to file denied on app's second run

I'm facing a weird issue, when trying to access file on SD card with code:我在尝试使用代码访问 SD 卡上的文件时遇到了一个奇怪的问题:

var path = @"D:\Test\test.txt";
try
{
    StorageFile file = await StorageFile.GetFileFromPathAsync(path);
}
catch (Exception ex) { Debug.WriteLine($"File access failed due to {ex.Message}"); }

path = @"Test\test.txt";
StorageFile file2 = await (await KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault().GetFileAsync(path);

The file is on SD card, I've declared RemovableStorage capability, and added FileTypeAssociation .该文件在 SD 卡上,我已经声明了RemovableStorage功能,并添加了FileTypeAssociation I can get the file when I first debug the app, but on the second run I get UnauthorizedException with the first StorageFile .我可以在第一次调试应用程序时获取该文件,但是在第二次运行时,我收到了第一个StorageFile 的UnauthorizedException Amazingly the second try to get the file via RemovableStorage works every time.令人惊讶的是,通过RemovableStorage获取文件的第二次尝试每次都有效。 If I only restart the phone and debug app once again - it will again work, but still only for the first time.如果我只重新启动手机并再次调试应用程序 - 它会再次工作,但仍然只是第一次。

Is accessing files by StorageFile.GetFileFromPathAsync() somehow limited?通过StorageFile.GetFileFromPathAsync()访问文件是否受到某种限制? Am I missing something?我错过了什么吗?


UPDATE:更新:

Seems like on newest version of emulator 10856 I get an exception on every run, what may mean that there will be no way to access file via full path.似乎在最新版本的模拟器 10856 上,每次运行时都会出现异常,这可能意味着无法通过完整路径访问文件。

The picker allows access because the user provided the file to you, not the path.选择器允许访问,因为用户向您提供了文件,而不是路径。 A winstore app should not be dependent upon a users drive / folder layout, that is why you don't have authority to access files by absolute path. winstore 应用程序不应依赖于用户驱动器/文件夹布局,这就是您无权通过绝对路径访问文件的原因。 In this case, we should firstly allow access to removable devices in manifest file, then we should use system's RemovebleDevices folder to access files of allowable types.在这种情况下,我们应该首先在清单文件中允许访问可移动设备,然后我们应该使用系统的 RemovebleDevices 文件夹访问允许类型的文件。

Also it is strange you can work with the absolute path the first time.同样奇怪的是,您可以第一次使用绝对路径。 In my test device, I always get the UnauthorizedAccessException and "Access is denied" error if using absolute path.在我的测试设备中,如果使用绝对路径,我总是会收到 UnauthorizedAccessException 和“访问被拒绝”错误。 My device is of build 10586.11.我的设备是构建 10586.11。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM