XAML 即使在Appmanifest中放置了broadfilesystemaccess,UWP应用程序仍可以访问文件

pkln4tw6  于 2022-12-16  发布在  Mac
关注(0)|答案(1)|浏览(165)

我正在做一个uwp应用程序,它需要访问用户从filepicker中选择的文件。我知道uwp运行在沙盒上,没有访问文件的权限。在谷歌上搜索了几个小时后,我发现我必须在应用程序清单中添加broadfilesystemaccess,并从设置中打开访问应用程序文件的权限。我做了所有这些,但我的应用程序仍然无法访问文件
这是我应用程序清单

<?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
            {

            };

我尝试删除所有其他功能,但也没有工作

ljo96ir5

ljo96ir51#

根据文档应用程序功能声明,提到此功能适用于Windows.Storage APIs它不适用于File.Open etc API。这就是此行为的原因。
如果你需要读取你得到的文件,你需要使用这个:

var stream = await virus.OpenAsync(Windows.Storage.FileAccessMode.Read);

请注意,the StorageFile.OpenAsync Method返回IRandomAccessStream,而不是FileStream

相关问题