unable to save settings in app.exe.config
app-config, c#, configuration
Solution
According to the 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 on the configuration file that you obtain by using one of the following Open methods:
OpenExeConfiguration
OpenMachineConfiguration
OpenMappedExeConfiguration
However, if you want to update app.config file, I would read it as an xml document and manipulate it as a normal xml document.
Please see the following example: Note: this sample is just for proof-of-concept. 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 "TimeStamp" attribute to MySettings in app.config to check the result easily.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySettings" type="TestApp.MySettings, TestApp"/>
</configSections>
<MySettings CustomerName="" EmailAddress="" TimeStamp=""/>
</configuration>
Problem
i am facing one problem. i want to save settings in app.config file i wrote separate class and defined section in config file.. but when i run the application. it does not save the given values into config 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 where i am saving the information in config 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 u tell me where is the error.. i tried alot and read from internet. but still unable to save information in config file.. i ran the application by double clicking on exe file also.