Question Configuration Manager

Dave1024

New member
Joined
Nov 19, 2020
Messages
2
Programming Experience
Beginner
Hi,

I'm trying to read and write entries to the configuration file. I have no issues in reading entries using:

var Value = ConfigurationManager.AppSettings["Key1"];

This will recall the entry in the appSettings portion of the DLL.config file.

However, I don't seem to be able to change this and write it back to file. The code I'm using is:
var NewValue = "NewTestValue";
ConfigurationManager.AppSettings.Set("Key1", NewValue);
ConfigurationManager.RefreshSection("appSettings");

It reads the value of Key1 &it sets correctly, I have tried getting it to pop up in a message box and it appears to change. When I check the config file or restart the app then it resets and the new value is not saved.

Any ideas?

Dave
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,135
Location
Chesapeake, VA
Programming Experience
10+
Read the documentation. Notice how Config.Save() is used to write out the configuration.
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,801
Location
UK
Programming Experience
10+
Read the documentation. Notice how Config.Save() is used to write out the configuration.
That's not how its recommended to do it in .NetCore. This is common in .Net web apps. Should this be moved?

Op wants to do this in .NetCore. You are however correct regarding the Config.Save call if its for a .Net web app. :p

I feel its important to mention that .NetCore doesn't use this method... as far as I recall. The method the OP is using is designed for .Net web apps. Isn't that why Microsoft moved to the json config file in .NetCore Skydiver?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,135
Location
Chesapeake, VA
Programming Experience
10+
Correct.

The idea in dotNetCore is that an app is not supposed to change the configuration. That's because the configuration passed in to the dotNetCore application is an amalgam of the settings read in from the appsettings.json, appsettings.<env>.json, environment variables, and the command line parameters. All those settings are just stored in essentially a POCO that is now disconnected from all the sources that were used to populate the POCO.
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,801
Location
UK
Programming Experience
10+
I just had to quote you since the docs you provided are for .Net Frameworks, and suitable for .Net web apps.

Just to be sure, @Dave1024 can you confirm which project you are running, because the course of the desired recommendation depends on it, and also depends if you are following the correct build technique?

C#:
ConfigurationManager.AppSettings.Set("Key1", NewValue);
ConfigurationManager.RefreshSection("appSettings");
That code is only meant to be used in .Net based web applications.
 

Dave1024

New member
Joined
Nov 19, 2020
Messages
2
Programming Experience
Beginner
Thanks for all the responses!

The application is currently a .NetCORE application although it doesn't need to be. On the basis that I need to write back to the config file I have recoded the application to use .Net 4.7.2. However the
Config.Save ()

Gives an error saying config does not exist in current context

Many thanks
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,135
Location
Chesapeake, VA
Programming Experience
10+
Read the documentation I linked to. In particular the sample code. Notice that config was a variable, not a class.
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,801
Location
UK
Programming Experience
10+
If you're doing this in .NetCore and not .Net Frameworks web app, you are doing it wrong. You've just gone and avoided what I said on post p1/#5
 
Top Bottom