Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a application in which I want to update app.config. And then read from it. But it's not happening, I'm getting the prior value not the updated value. My code like this :
C#
class Program
    {
	static string str= ConfigurationManager.AppSettings["UserID"];	// it returns "hello"
        static string key = "UserID";
        static string value = NewValue();
        static string exeFileName = "ConsoleApplication.exe";
        static void Main(string[] args)
        {
                 Console.WriteLine(str);		
                 UpdateConfig(key, value, exeFileName);
                str= ConfigurationManager.AppSettings["UserID"];
		Console.WriteLine(str);
		// Doing other stuff
	}
	private static string NewValue()
	{
		return "HelloWorld";
	}
	private static void UpdateConfig(string key, string value, string exeFileName)
        {
            try
            {
                Configuration configFile = ConfigurationManager.OpenExeConfiguration(exeFileName);
                configFile.AppSettings.Settings[key].Value = value;
                configFile.Save();
            }
            catch (Exception ex)
            { 
                throw ex; 
            }
        }
	// Here some others functions
   }


Both time I'm printing "hello" not "HelloWorld".
Please Help me.
Thanks in advance.
Posted

Hi,

try like this..
C#
// change the UpdateConfig function like below.
private static void UpdateConfig(string key, string value)
{
    try
    {
        Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        configFile.AppSettings.Settings[key].Value = value;
        configFile.Save();
    }
    catch (Exception ex)
    { 
        throw ex; 
    }
}


instead of exefilename, try by using ConfigurationUserLevel.None.

refer ConfigurationManager.OpenExeConfiguration Method (String) - MSDN[^]

hope it helps.
 
Share this answer
 
Comments
Member 9330747 26-Apr-13 4:17am    
it's now working. i'm facing the same problem.
Karthik Harve 26-Apr-13 4:37am    
still settings are not reflecting ??
hi try this,

C#
Key = txtKServerName.Text;
                    Value = txtVServerName.Text;
                   XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
            try
            {
                if (KeyExists(Key))
                {
                    MessageBox.Show("Key name:" + Key + "already exists in the configuration.");
                }
                else
                {
                    XmlNode newChild = appSettingsNode.FirstChild.Clone();
                    newChild.Attributes["key"].Value = Key;
                    newChild.Attributes["value"].Value = Value;
                    appSettingsNode.AppendChild(newChild);
                    xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
                    xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                    MessageBox.Show("Added Successfully ");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
Share this answer
 
v2
This code will edit your app.config:
C#
var cfg = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
cfg.AppSettings.Settings.Add("test1", "test2");
cfg.Save();

Make sure you check tha correct config file when you look for changes. Not the one in your project, but the one in your bin folder ;)
 
Share this answer
 
Comments
Member 9330747 26-Apr-13 4:22am    
should i mention the config file path like ("..\\bin\\Debug\\app.config") ? if yes, how ? please reply.
StianSandberg 26-Apr-13 4:29am    
no, it's not necessary. Assembly.GetExecutingAssembly().Location returns the absolute path to your .exe and by using that path, it will find the corresponding .config file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900