【问题标题】:UWP write to InstalledLocation gets access is deniedUWP 写入 InstalledLocation 获取访问被拒绝
【发布时间】:2019-02-05 22:05:23
【问题描述】:

当我尝试写入应用程序安装目录中的文件夹中的文件时,我收到“访问被拒绝”。

有趣的是我可以很好地阅读文件。然后,当我测试写入它时,我得到了错误。

我使用 NOTEPAD 在那里创建了 sample.txt 文件。

目标:

这样做的原因是我的 Windows 窗体应用程序可以与 UWP 应用程序交换文件。我需要在 Windows 应用程序可以找到的文件夹中进行交换;这是固定的(常数)。我虽然也许 uwp 安装文件夹可以工作。

这个应用程序进入我们的产品,而不是“商店”——没有外部连接。理想情况下,我希望完全访问 Windows 10。

这是我的 UWP C# 测试代码....

这是相同的代码,只是非常像......

   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");

我尝试修改清单以启用broadFileSystemAccess,但出现以下错误.......

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

【问题讨论】:

    标签: c# visual-studio uwp


    【解决方案1】:

    这是设计使然。安装位置受写保护,以确保安装的完整性并允许增量更新以及彻底卸载。

    您应该改为写入 ApplicationData。
    https://docs.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data

    【讨论】:

    • 这会是一条固定路径吗?我们的 Windows 窗体应用程序需要与 UWP 应用程序以可预测的路径交换文件,两者都可以进行读写。
    • 对于给定的包名,它是常量。如果您更改包名称,则路径会更改。为了更加健壮,您可以从 UWP 公开一个 AppService 端点,Winforms 应用可以查询该端点以从 UWP 获取有效的 AppData 路径。
    【解决方案2】:

    这没什么好笑的,这就是每个操作系统(至少每个主要操作系统,如 Windows、macOS、iOS、Android)的工作原理。当然,在某些情况下,如果您是管理员,您可能在某些操作系统上拥有写入每个文件夹的权限,有时甚至有一些额外的权限,但基本上,即使在比 UWP 限制较少的环境(如 WPF 或 macOS)上,用户也无法做到这一点.

    如果用户有权访问该文件夹,您可以使用某些文件系统选择器请求该权限。您还可以在清单中要求broadFileSystemAccess,您将有权访问用户可以访问的所有文件夹。但问题仍然存在,因为一般用户在没有一些额外分配权限的情况下无法访问 UWP 安装文件夹。

    因此,您应该考虑将数据写入其他地方。

    【讨论】:

    • 我添加了 broadFileSystemAccess 到清单但得到错误(见上文)。此 UWP 应用程序将在我们的嵌入式产品(非商店)中运行,未连接任何东西,并且需要与 Windows 窗体应用程序共享文件。所以UWP需要读写一些文件夹,安装或者其他是windows forms app可以访问的固定路径。
    猜你喜欢
    • 2018-05-17
    • 2019-06-28
    • 2021-09-18
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多