错误消息:访问路径 'C:\Program Files\WindowsApps 被拒绝

Error Message: Access to the path 'C:\Program Files\WindowsApps is denied

我通过 Windows 商店向自己分发了 xaf Winforms+Desktop Bridge 应用程序。

following this blog

从博客中,我想 pre-generate ModelAssembly.dll、Model.Cache.xafml 和 ModulesVersionInfo 文件并将它们打包到输出目录中。

这些文件如果不存在则由 XAF 生成。

输出目录由

给出
Path.GetDirectoryName(GetType().Assembly.Location)

当我从商店安装和 运行 我的应用程序时。我收到此错误消息。

Access to the path 'C:\Program Files\WindowsApps\JobTalk.JobTalk_1.0.19.0_x64__8kpaqbvntb9aj\SBD.JobTalk.Workflow.Win10\ModelAssembly.dll' is denied.

在代码中我尝试寻找形式 Path.GetRelativePath 但它在 Framework 4.7.2 中不可用

简而言之,UWP 应用程序不允许通过绝对路径访问文件。因为 UWP 应用程序 运行 在沙盒中并且对应用程序容器外的文件的访问非常有限。在大多数情况下,UWP 应用程序只能直接访问其安装文件夹和应用程序数据文件夹。由于转换后的应用也是UWP应用,所以你的应用需要遵循这个规则。

请检查Prepare to package a desktop application。文档中提到了一些与您的问题相关的场景。

Your application writes to the AppData folder or to the registry with the intention of sharing data with another app. After conversion, AppData is redirected to the local app data store, which is a private store for each UWP app.

Your application writes to the install directory for your app. For example, your application writes to a log file that you put in the same directory as your exe. This isn't supported, so you'll need to find another location, like the local app data store.

这里有一个特殊的场景,你可以通过文件绝对路径访问文件。这是一种称为 broadFileSystemAccess 的受限功能。添加 broadFileSystemAccess 功能后,您可以使用 Windows.Storage namespace to get all files that the user has access to. Please read File access permissions 中的 API 获取更多信息。

请注意文档中有说明:

If you submit an app to the Store that declares this capability, you will need to supply additional descriptions of why your app needs this capability, and how it intends to use it.

Xavier 的回答解释了问题所在。它发生在我的项目中的原因是我错过了将链接文件标记为 "Copy if newer"

的步骤