在 CrossPlatform UWP 中访问内部计算机驱动器的文件和文件夹

Access files and folders of Internal Computer drives in CrossPlatform UWP

制作一个适用于所有系统的 class 库,并围绕存储进行解析 (system.io)。 到目前为止,它适用于 Winforms 和 Wpf,但无法在 UWP 上运行。 我还有总共 3 个内部存储驱动器

C:/
D:/
E:/

我可以从 C 盘获取文件和文件夹,但不能从 D 或 E 盘获取。 我已将其添加到 .UWP Package.appxmanifest

xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">`
    <Capabilities>
        <rescap:Capability Name="broadFileSystemAccess"/>
        <uap:Capability Name="musicLibrary"/>
        <uap:Capability Name="removableStorage"/>
        <uap:Capability Name="picturesLibrary"/>
        <uap:Capability Name="videosLibrary"/>
        <uap:Capability Name="documentsLibrary"/>
      </Capabilities>`

但是 给我一个警告

Severity Code Description Project File Line Suppression State Warning The element 'Capabilities' in namespace 'http://schemas.microsoft.com/appx/manifest/foundation/windows10' has invalid child element 'Capability' in namespace 'http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities'. List of possible elements expected: 'CapabilityChoice' in namespace 'http://schemas.microsoft.com/appx/manifest/foundation/windows10' as well as 'Capability' in namespace 'http://schemas.microsoft.com/appx/manifest/uap/windows10' as well as 'Capability' in namespace 'http://schemas.microsoft.com/appx/manifest/foundation/windows10' as well as 'Capability' in namespace 'http://schemas.microsoft.com/appx/manifest/uap/windows10/4' as well as 'Capability' in namespace 'http://schemas.microsoft.com/appx/manifest/uap/windows10/6' as well as 'Capability' in namespace 'http://schemas.microsoft.com/appx/manifest/uap/windows10/7' as well as 'Capability' in namespace 'http://schemas.microsoft.com/appx/manifest/uap/windows10/3' as well as 'Capability' in namespace 'http://schemas.microsoft.com/appx/manifest/uap/windows10/2' as well as 'CustomCapabilityChoice' in namespace 'http://schemas.microsoft.com/appx/manifest/foundation/windows10' as well as 'CustomCapability' in namespace 'http://schemas.microsoft.com/appx/manifest/uap/windows10/4' as well as 'Dev....

我认为这意味着 broadFileSystemAccess 无法正常工作。 UWP 应用程序已显示在文件系统的系统设置中,我已允许它但仍然失败。

使用的文件路径示例是

@"D:\Games\Genshin Impact/7z.dll"
@"D:\Games\Genshin Impactz.dll"
"D:\Games\Genshin Impact\7z.dll"

使用了这两个文件库

System.IO

File & Folder

Windows.Storage

StorageFile & StorageFolder

我曾尝试使用 File/Folder 选择器,它偶尔会起作用,但我想尽量避免使用它们,因为这意味着是一个 .DLL,可以像 System.IO.File/Folder 和其他图书馆。

非常感谢任何帮助。

编辑: 弄清楚了,我不得不删除其他与存储相关的功能。

<uap:Capability Name="musicLibrary"/>
<uap:Capability Name="removableStorage"/>
<uap:Capability Name="picturesLibrary"/>
<uap:Capability Name="videosLibrary"/>
<uap:Capability Name="documentsLibrary"/>

之后,我构建、停止它、取消选中并重新检查系统设置中的文件权限,然后构建成功。

你接近隧道尽头的光。您需要使用 Windows.Storage api 来访问路径为 () 的文件。 如果要访问带有路径的文件,请调用 Windows.Storage.StorageFile.GetFileFromPathAsync 方法。根据您的情况,请使用DependencyService to call the native GetFileFromPathAsync method and then return file data that could use in forms directly. You could refer to this case .

想通了,我不得不删除其他与存储相关的功能。

<uap:Capability Name="musicLibrary"/>
<uap:Capability Name="removableStorage"/>
<uap:Capability Name="picturesLibrary"/>
<uap:Capability Name="videosLibrary"/>
<uap:Capability Name="documentsLibrary"/>

在那之后,我构建、停止它、取消选中并重新检查系统设置中的文件权限,而不是构建并且它起作用了。