Loading XML config file not working in MSIX Packages
When deploying software, configuration files are the key to customizing behavior without modifying the source code.
But, deploying an app using an MSIX package may lead to a config file loading issue.
Don’t worry! This article provides clear solutions to address this common MSIX packaging challenge, specifically for .NET applications.
First, let's discuss when this error usually occurs.
Why isn’t the config file loading correctly?
While debugging in Visual Studio, the config file might load correctly. But after installation via an MSIX package, the working directory defaults to System32. If a relative path is used without specifically setting the working directory to the executable folder, loading the config file will fail.
Here are two efficient solutions:
- Set the working directory - use Advanced Installer to build your MSIX package and set it as the Windows Application Packaging Project does not have support for this. It is a great option if you do not have access to the source code or you simply prefer not to modify it. You can set the working directory from the Advanced Installer interface, no coding required.
- Specify the full path for the config file as param - if you do not want to set the working directory from the MSIX package, you will have to modify the source code of your application. Specifically, you have to get the location of the binary and append the config file name to this location, and you will end up with the full path of the config file.
Set the working directory using Advanced Installer
Advanced Installer provides a user-friendly interface that simplifies creating the MSIX packages. You can easily change the working directory from its GUI.
Steps to Change Working Directory in Advanced Installer:
- Create your project in Advanced Installer.
- Navigate to the Application Details tab.
- Locate the Start Point section and set the Working directory to the executable folder.
Need a detailed tutorial? Check how to Create an MSIX package using Advanced Installer.
Specify the full path for the config file as param
If you prefer not to change the working directory from the MSIX package project, you can fix the issue by modifying the source code.
You can create the MSIX package using Visual Studio - no need for a third-party app. All you need to do is change the path used to load the configuration file.
Steps to Set the Config File Path:
1. Get the install location of the binary.
string exeDir=Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
2. Append the config file name to the string obtained in the previous step. Then, you can use the new path to load the config file.
string configFilePath = "./config.xml"; string newPath = Path.Combine(exeDir, configFilePath);
By following this method, the config file will be loaded from the full path, independent of the working directory.
Video
Conclusions
Deploying an application using MSIX package can cause issues with loading a configuration file due to the default working directory. These problems can be resolved using one of the two solutions presented above.
Whether you're an IT professional or developer in the application packaging industry, these step-by-step guidelines provide clarity to efficiently overcome this challenge.