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