I am making a uwp app which needs to access files which user has picked from filepicker. I know uwp is running on a sandbox and don't have permissions to access files. After hours of searching on google i found that i have to add broadfilesystemaccess in the app manifest and turn on the permission for accessing files for the app from settings. I did all of this but my app still can't access the files
here is my 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>
file picker code
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
{
};
I tried removing all other capabilities but that too didnt worked
CodePudding user response:
Based on the document App capability declarations, it is mentioned that This capability works for Windows.Storage APIs. It does not work with File.Open etc
API. This is the reason for this behavior.
If you need to read the file you got, you need use this:
var stream = await virus.OpenAsync(Windows.Storage.FileAccessMode.Read);
Please note, the StorageFile.OpenAsync Method returns an IRandomAccessStream
not a FileStream