Search code examples
c#configuration-files

How do I share configuration settings between two applications


I have two applications written in C#. Both do the same basic task; one has a UI and is designed to set-up and configure a process and the other is a console application designed to run the process as configured on a schedule.

I have them set up in Visual Studio as two different projects in the same Solution with a separate library they both reference doing all the hard work. Having a single application work in both circumstances doesn't seem to be a recommended approach, so I have split the UI/Console variants out accordingly.

The problem is that I am trying to share configuration - just a few string values; connection strings, file locations and so on - between them. This is set up from the UI version but the Console project can't run without it.

Initially I just had the settings in my Application.exe.config but when I installed the application using a regular installer, it died the moment it tried to save the configuration file because of file permissions.

Now I have a Settings object in the shared library with these values as User Settings made accessible through a wrapper class that both other projects can access and update. That solves my update problem, but now my Console application can't read the settings values that have been set by the UI.

I could just drop them in a configuration file and handle it manually - that would probably have been a little faster by this point than trying to do it the Windows way - but I feel like this must be something that the platform supports somehow.

How should I store my configuration so it is available to two different applications (created within the same Visual Studio Solution) and the settings used by both applications can be updated and saved by the user?


Solution

  • Use the ConfigurationManager class to manage your app.config (see the docs here) if you want the built-in way. You may need to run your UI with elevated privilege to edit the config or you can make the config files user specific and stash them under each user's profile.