0

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?

Toby
  • 517
  • 1
  • 5
  • 15

2 Answers2

1

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.

Bogdan Mitrache
  • 10,536
  • 19
  • 34
0

@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.

Toby
  • 517
  • 1
  • 5
  • 15