1
votes

I'm trying to figure out what the best practice is for updating an app.config file of an existing application to include new app settings, without overwriting pre-existing settings values.

So, to start from the beginning, I have a project that gets built into an MSI by a Visual Studio setup project, and that MSI is used to install the application. This application is already installed at probably 1000+ locations. Now, in a new version of the application, we have a new app setting to define a webservice endpoint. The setup project, however, will not deploy the new app.config with the new setting, because setup projects are dumb like that and they will only install an app.config if either it doesn't already exist, or if the existing one has matching created and modified dates, to avoid overwriting user defined configuration.

Now, since I can't overwrite the app.config, I figured that maybe I will deploy the app.config changes as a separate XML file, and at application startup, I will load this separate XML file, copy all the settings out of it, compare to what's in the actual app.config file, and anything that's missing I will save into the app.config file.

But I'm having problems loading config from an external XML file and copying it over to the app.config. How would I go about doing this? Is there any other better way to manage updating an app.config file? What is best practice?

I'm more familiar with Java, and this would be a very simple thing to do with Properties files, but doesn't seem so simple with .NET config files.

2

2 Answers

1
votes

There's no out-of-the-box method as far as I know.

I'd do it by parsing the file into an XDocument, then checking whether doc.Element("settingName") is null. If it is, then that setting doesn't exist and you can add a default value. If the node does exist, the the user has defined a custom setting.