MSIX发布 – 加载XML配置文件不起作用

huangapple go评论88阅读模式
英文:

MSIX publish - Loading XML config file not working

问题

I am using Visual Studio 2022 to create a .NET 7 application.

我正在使用Visual Studio 2022创建一个.NET 7应用程序。

I have a config.xml file in my project, that my app loads.

我的项目中有一个config.xml文件,我的应用程序加载它。

When I build and run the application normally, all is fine.

当我正常构建和运行应用程序时,一切正常。

However, the XML file will not load when I produce and run my application from an MSIX bundle.

然而,当我从MSIX捆绑包中生成并运行应用程序时,XML文件无法加载。

Here is my project structure:

这是我的项目结构:

-> Solution ----> Project_a (Main entry point) ----> Project_b (DLL) ------> my_config.xml ----> MSIX_APPLICATION

-> 解决方案 ----> 项目_a(主入口点) ----> 项目_b(DLL) ------> my_config.xml ----> MSIX_APPLICATION

I have a post-build event that copies my_config.xml from Project_b into Project_a's build folder.

我有一个后构建事件,它将my_config.xml从Project_b复制到Project_a的构建文件夹。

As mentioned, all works fine when running my application normally, but not when running via an MSIX App Bundle. it won't find and load my_config.xml.

正如我所提到的,当正常运行我的应用程序时,一切正常,但当通过MSIX应用程序捆绑包运行时,它无法找到并加载my_config.xml。

When I go into the location MSIX installs it to, c:\program_files\WindowsApp\<myapp> directory, I can see the my_config.xml file is there alongside the binary. If I run the main binary from there, it seems to load the XML file fine.

当我进入MSIX安装到的位置,c:\program_files\WindowsApp\<myapp>目录时,我可以看到my_config.xml文件与二进制文件一起存在。如果我从那里运行主要的二进制文件,它似乎可以正常加载XML文件。

Any idea's?

有什么想法吗?

英文:

I am using Visual studio 2022, to create a .NET 7 application.

I have a config.xml file in my project, that my app loads. When I build and run the application normally, all is fine. However, the XML file will not load when I produce and run my application from an MSIX bundle.

Here is my project structure:

-> Solution
----> Project_a  (Main entry point)
----> Project_b  (DLL)
------> my_config.xml
----> MSIX_APPLICATION

I have a post build event, that copies my_config.xml from Project_b, into project_a's build folder.

As mentioned, all works fine when running my application normally, but not when running via a MSIX App Bundle. it wont find and load my_config.xml.

When I go into the location MSIX installs it to, c:\program_files\WindowsApp\<myapp> directory, I can see the my_config.xml file is there alongside the binary. If I run the main binary from there, it seems to load the XML file fine.

Any idea's?

答案1

得分: 1

This seems to be the case of running the EXE with a different/incorrect current working directory.

如果我记得正确,默认情况下通过MSIX安装的程序将以System32作为默认工作目录启动,除非另有指定。

尝试将安装文件夹指定为工作目录,看看是否有帮助。

如果您无法访问源代码,那么您可以使用第三方应用程序,如Advanced Installer,它具有自动的PSF集成(以及构建流水线工具)。在Advanced Installer中,您可以从其图形界面设置工作目录,而无需编写任何代码。(在链接的文章中搜索工作目录,您将找到有关如何使用Advanced Installer或MS工具进行设置的信息)

免责声明:我是Advanced Installer团队的成员。

英文:

This seems to be the case of running the EXE with a different/incorrect current working directory.

If I remember correctly, by default installed via an MSIX will launch with System32 as the default working dir, if not specified otherwise.

Try specifying your install folder as the working dir, and see if that helps.

If you don't have access to the source code then you can use a third-party application like Advanced Installer which has automatic PSF integration (and build pipeline tools). Within Advanced Installer you can set the working directory from its GUI, without being required to write any code. (Search for working directory in the linked articles and you will find info on how you can use Advanced Installer or the MS tool to set it)

Disclaimer: I work on the team building Advanced Installer.

答案2

得分: 0

@Bogdan Mitrache 提醒我存在问题,并建议了正确的答案。

问题是,当您将应用程序作为 MSIX WinApp 包运行时,当前工作目录设置为 c:\windows\system32,而不是二进制文件实际位于计算机上的位置。那可能是类似于 C:\Program Files\WindowsApps\f41f91a1-90e0-4fc7-a429-004ed3423fdd_0.1.1.0_x64__89c53gqcdr1he\Your_app_name

我所做的是,而不是尝试在我的代码中加载 ./my_config.xml,我首先获取了二进制文件的安装位置。您可以这样做:

string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

然后,我将我的 xml 文件名与该字符串连接起来,并将其用作加载的路径。

希望这对某人有所帮助。

英文:

@Bogdan Mitrache tipped me off to the problem, and suggested the right answer.

The issue was that when your running your app as an MSIX WinApp Package, the current working directory is set to c:\windows\system32, and not where the binary is actually located on your computer. That would be something like C:\Program Files\WindowsApps\f41f91a1-90e0-4fc7-a429-004ed3423fdd_0.1.1.0_x64__89c53gqcdr1he\Your_app_name.

What I did, is instead of trying to load ./my_config.xml in my code, I first got the location of where the binary was installed. You can do that like this:

string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Then I concatenated my xml file name to that string, and used that as the path to load it from.

I hope this helps someone.

huangapple
  • 本文由 发表于 2023年6月12日 00:26:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定