Failed to save settings to app.exe.config

I ran into one problem.

I want to save the settings in the app.config file

I wrote a separate class and a specific section in the configuration file.

but when i run the application. it does not save the setpoints in the configuration file

here is SettingsClass

public class MySetting:ConfigurationSection { private static MySetting settings = ConfigurationManager.GetSection("MySetting") as MySetting; public override bool IsReadOnly() { return false; } public static MySetting Settings { get { return settings; } } [ConfigurationProperty("CustomerName")] public String CustomerName { get { return settings["CustomerName"].ToString(); } set { settings["CustomerName"] = value; } } [ConfigurationProperty("EmailAddress")] public String EmailAddress { get { return settings["EmailAddress"].ToString(); } set { settings["EmailAddress"] = value; } } public static bool Save() { try { System.Configuration.Configuration configFile = Utility.GetConfigFile(); MySetting mySetting = (MySetting )configFile.Sections["MySetting "]; if (null != mySetting ) { mySetting .CustomerName = settings["CustomerName"] as string; mySetting .EmailAddress = settings["EmailAddress"] as string; configFile.Save(ConfigurationSaveMode.Full); return true; } return false; } catch { return false; } } } 

and this is the code from which I save the information in the configuration file

 private void SaveCustomerInfoToConfig(String name, String emailAddress) { MySetting .Settings.CustomerName = name; MySetting .Settings.EmailAddress = emailAddress MySetting .Save(); } 

and this is app.config

 <configuration> <configSections> <section name="MySettings" type="TestApp.MySettings, TestApp"/> </configSections> <MySettings CustomerName="" EmailAddress="" /> </configuration> 

Can you tell me where the error is. I tried and read a lot from the Internet. but still unable to save the information in the configuration file.

I launched the application by double-clicking the exe file.

+7
c # configuration app-config
source share
4 answers

According to MSDN: ConfigurationManager.GetSection Method ,

The ConfigurationManager.GetSection method accesses run-time configuration information that it cannot change . To change the configuration, you use the Configuration.GetSection method in the configuration file that you receive using one of the following Open methods:

However, if you want to update the app.config file, I would read it as an XML document and process it as a regular XML document.

See the following example: Note. This sample is for proof of concept only. It should not be used in production as it is.

 using System; using System.Linq; using System.Xml.Linq; namespace ChangeAppConfig { class Program { static void Main(string[] args) { MyConfigSetting.CustomerName = "MyCustomer"; MyConfigSetting.EmailAddress = "MyCustomer@Company.com"; MyConfigSetting.TimeStamp = DateTime.Now; MyConfigSetting.Save(); } } //Note: This is a proof-of-concept sample and //should not be used in production as it is. // For example, this is not thread-safe. public class MyConfigSetting { private static string _CustomerName; public static string CustomerName { get { return _CustomerName; } set { _CustomerName = value; } } private static string _EmailAddress; public static string EmailAddress { get { return _EmailAddress; } set { _EmailAddress = value; } } private static DateTime _TimeStamp; public static DateTime TimeStamp { get { return _TimeStamp; } set { _TimeStamp = value; } } public static void Save() { XElement myAppConfigFile = XElement.Load(Utility.GetConfigFileName()); var mySetting = (from p in myAppConfigFile.Elements("MySettings") select p).FirstOrDefault(); mySetting.Attribute("CustomerName").Value = CustomerName; mySetting.Attribute("EmailAddress").Value = EmailAddress; mySetting.Attribute("TimeStamp").Value = TimeStamp.ToString(); myAppConfigFile.Save(Utility.GetConfigFileName()); } } class Utility { //Note: This is a proof-of-concept and very naive code. //Shouldn't be used in production as it is. //For example, no null reference checking, no file existence checking and etc. public static string GetConfigFileName() { const string STR_Vshostexe = ".vshost.exe"; string appName = Environment.GetCommandLineArgs()[0]; //In case this is running under debugger. if (appName.EndsWith(STR_Vshostexe)) { appName = appName.Remove(appName.LastIndexOf(STR_Vshostexe), STR_Vshostexe.Length) + ".exe"; } return appName + ".config"; } } } 

I also added the TimeStamp attribute for MySettings in app.config to easily check the result.

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="MySettings" type="TestApp.MySettings, TestApp"/> </configSections> <MySettings CustomerName="" EmailAddress="" TimeStamp=""/> </configuration> 
+5
source share

In many cases (in a limited user scenario), the user does not have direct access to the application configuration file. To get around this, you need to use user settings.

If you right-click your project and select the tab with the name "Settings", you can make a list of the settings that are stored in the configuration file. If you select "Region" for "User", this parameter is automatically saved in the configuration file, using a type that will automatically save the settings in the users AppData area, where the user always has write access. Settings are also automatically provided as properties created in the properties code file \ Settings.Designer.cs, and are available in your code in Properties.Settings.Default .

Example:

Suppose you add a custom parameter named CustomerName :

When loading the application, you will need to return the value from the saved setting (either by default, as in the application configuration, or if it is stored for this user, the configuration file for the user):

 string value = Properties.Settings.Default.CustomerName; 

If you want to change the value, just write to it:

 Properties.Settings.Default.CustomerName = "John Doe"; 

If you want to save all the settings, just call Save:

 Properties.Settings.Default.Save(); 

Please note that during development, the user file will have a reset value by default, but this only happens when creating the application. When you simply launch the application, the settings you saved will be read from the user configuration file for the application.

If you still want to create your own settings, you can try it once and see what VisualStudio has automatically created for you to get an idea of ​​what you need for this.

+3
source share

To really update the configuration file, you need to call .Save() on the Configuration object, and not just on your configuration section object.

You should check out the three-part series of Jon Rista for the .NET 2.0 configuration on CodeProject.

Highly recommended, well written and very helpful! It shows all aspects of working with .NET 2.0 and configuration, and helped me a lot to understand this issue.

Mark

+2
source share

Keep in mind that appname.vshost.exe.Config returns to its original state at the end of the debugging session. This way you can save the file (you can check with Notepad at runtime) and then lose the saved contents when debugging stops.

+2
source share

All Articles