app.config files..

C

Chizl

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
 
D

dlm@bypsoft

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top