我正在制作一個 uwp 應用程式,它需要訪問用戶從檔案選擇器中選擇的檔案。我知道 uwp 在沙箱上運行并且沒有訪問檔案的權限。在谷歌上搜索了幾個小時后,我發現我必須在應用程式清單中添加廣泛的檔案系統訪問權限,并從設定中打開訪問應用程式檔案的權限。我做了所有這些,但我的應用程式仍然無法訪問檔案
這是我的應用清單
<?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
{
};
我嘗試洗掉所有其他功能,但這也沒有奏效
uj5u.com熱心網友回復:
根據檔案App 功能宣告,提到此功能適用于Windows.Storage API。它不適用于File.Open etcAPI。這就是這種行為的原因。
如果你需要閱讀你得到的檔案,你需要使用這個:
var stream = await virus.OpenAsync(Windows.Storage.FileAccessMode.Read);
請注意,StorageFile.OpenAsync 方法回傳一個IRandomAccessStreamnot aFileStream
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533318.html
標籤:C#xmluwp
