I can read .config files all day, but for some reason I cant write to them..
After searching for a few days, there are some that have code showing to
write, but none of them seem to work. Someone explain to me what could be
the problem?
I'm admin of the machine, I'm writting local, and this is the code I've
tried..
Way #1
--------------
Configuration config;
config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[strKey].Value = strValue; //error, Object
reference not set to an instance of an object
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Way #2
--------------
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationManager.AppSettings.Add(strKey, strValue); //error,
config is readonly.. But it's not..
ConfigurationManager.RefreshSection("appSettings");
dlm@bypsoft - 06 Mar 2008 17:35 GMT
Hi,
we are performing such a tasks with the following:
string sPath = System.IO.Path.Combine(Environment.CurrentDirectory,
"your_exe_name.exe");
Configuration config = ConfigurationManager.OpenExeConfiguration(sPath);
config.AppSettings.Settings["test_key"].Value = "test_value";
config.Save(ConfigurationSaveMode.Modified);
If your <appSettings> section doesn't contain "test_key" you should add it
first (othewise you get null reference exception)
config.AppSettings.Settings.Add("test_key", "test_value");
and than save it.
This should work.
Regards,
dlm@bypsoft
Compare database schema and data on SQL Server, MySQL, Oracle for free
www.bypsoft.com
>I can read .config files all day, but for some reason I cant write to
>them.. After searching for a few days, there are some that have code
[quoted text clipped - 20 lines]
> config is readonly.. But it's not..
> ConfigurationManager.RefreshSection("appSettings");
Chizl - 20 Mar 2008 01:02 GMT
Is this too hard of a question or not worth anyones time?
>I can read .config files all day, but for some reason I cant write to
>them.. After searching for a few days, there are some that have code
[quoted text clipped - 20 lines]
> config is readonly.. But it's not..
> ConfigurationManager.RefreshSection("appSettings");
Peter Duniho - 20 Mar 2008 02:30 GMT
> Is this too hard of a question or not worth anyones time?
I suspect it's more to do with the vague nature in which you asked your
question. On occasion, someone will take some time to try to help someone
work through their question, to try to make it more clear and specific so
that it's possible to answer.
But often times, a poorly-asked question just goes unanswered. Such is
life. It's a good reason to learn to ask better questions.
Pete