Application Settings in .Net Core (WPF) and single exe

Viewed 1144

To migrate an application I want to continue using the plain old settings that still come along with .Net5 (App.Config, Settings.settings and so on).
I need a simple built in solution without additional dependencies.

The proposed way seems to be appsettings.json or similar.
For that to use with WPF you need to add some additonal dependencies which bloat the project when publishing it as single exe (not self contained). It is over-the-top for simple applications.

I followed the steps here: Equivalent to UserSettings / ApplicationSettings in WPF dotnet core The accepted answer from Alexander works for a normal exe built.

These are the generated files
MyApp.dll
MyApp.dll.config
MyApp.exe

Modifying "MyApp.dll.config" with an editor directly reflects the changed data in the code.

MessageBox.Show(Settings.Default.SomeConfigValue);

The used config file can be displayed using

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MessageBox.Show(config.FilePath);

It displays
C:\Projekte\MyApp\MyApp\bin\Debug\net5.0-windows\MyApp.dll.config

Unfortunately this fails for single exe

These are the generated files (in publish folder)
MyApp.dll.config
MyApp.exe

Modifying "MyApp.dll.config" or "MyApp.exe.config" has no effect. The file does not seem to be used by the framework.
Above messagebox now shows
C:\Projekte\MyApp\Publish<Unknown>.config

How to get the built-in configuration system work with single exe?

0 Answers
Related