简体   繁体   中英

UWP write to InstalledLocation gets access is denied

I get "access is denied" when I try to write to a file within a folder that is within the app's installation directory.

Funny thing is that I can read the file just fine. Then, when I test writing to it, I get the error.

I used NOTEPAD to create the sample.txt file there.

GOAL:

The reason for this is so my windows forms app can exchange files with the UWP app. I need to do the exchange in a folder that the windows app can find; that is fixed (constant). I though maybe the uwp install folder would work.

This app goes into our product, not to the 'store' -- no external connections. Ideally, I want full access to Windows 10.

Here's my UWP C# test code.... 在此处输入图片说明

Here's the same code, just all pretty like...

   StorageFolder mobile_upload_StorageFolder=null;

StorageFolder app_installedLocation_StorageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

string mobile_folder_name =               "FOR_MOBILE_UPLOAD";
 mobile_upload_StorageFolder = await app_installedLocation_StorageFolder.CreateFolderAsync(mobile_folder_name, CreationCollisionOption.OpenIfExists  );
Windows.Storage.StorageFile sampleFile = await mobile_upload_StorageFolder.GetFileAsync("sample.txt");

string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
Debug.WriteLine( "text: " +  text );
 await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "WRITTEN BY UWP");

I tried modifying the manifest to enable broadFileSystemAccess but get the following error...…….

    <?xml version="1.0" encoding="utf-8"?>
<Package xmlns:rescap="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" IgnorableNamespaces="uap mp uap5">
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="uap mp uap5 rescap">

    <Identity Name="Microsoft.SDKSamples.CameraFrames.CS" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" Version="1.0.0.0" />
  <mp:PhoneIdentity PhoneProductId="2344b9de-5071-42a6-8873-7fdeb38d53dd" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
  <Properties>
    <DisplayName>Camera Frames C# Sample</DisplayName>
    <PublisherDisplayName>Microsoft Corporation</PublisherDisplayName>
    <Logo>Assets\StoreLogo-sdk.png</Logo>
  </Properties>
  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.15063.0" MaxVersionTested="10.0.17134.0" />
  </Dependencies>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="CameraFrames.App">
      <uap:VisualElements DisplayName="MOVANO System Hub Camera  interface" Square150x150Logo="Assets\SquareTile-sdk.png" Square44x44Logo="Assets\SmallTile-sdk.png" Description="Camera Frames C# Sample" BackgroundColor="#00b2f0">
        <uap:SplashScreen Image="Assets\Splash-sdk.png" />
        <uap:DefaultTile>
          <uap:ShowNameOnTiles>
            <uap:ShowOn Tile="square150x150Logo" />
          </uap:ShowNameOnTiles>
        </uap:DefaultTile>
      </uap:VisualElements>
    </Application>
  </Applications>
  <Capabilities>
    <rescap:Capability Name="broadFileSystemAccess" />  CAUSES "the 'name' attribute is not valid"
    <Capability Name="privateNetworkClientServer" />
    <Capability Name="internetClientServer" />
    <DeviceCapability Name="microphone" />
    <DeviceCapability Name="webcam" />
  </Capabilities>
</Package>

This is by design. The install location is write-protected in order to ensure integrity of the installation and allow for incremental updates as well as clean uninstall.

You should write to your ApplicationData instead.
https://docs.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data

This isn't anything funny, this is how every operating system (at least every major like Windows, macOS, iOS, Android) works. Of course you may have in some situations on some operating systems right to write to every folder if you are administrator and sometimes have even some additional preveliges, but basically a user cannot do that even on less restricted environments than UWP (like WPF or macOS).

If user has the right to access that folder you may ask for the right by using some file system picker. You may also ask for broadFileSystemAccess in your manifest and you'll have the access to all folders that user can access. But the problem still remains as a general user can't access UWP install folder without some additional assigned rights.

So you should consider writing your data elsewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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