How to read and write settings to App.config(Configuration in C# .NET 2.0)

It's really simple but not clear in MSDN.

Read

Write

Hope this helps.

Feel free to subscribe to our RSS feed.

Comments

Very helpful thanks.

One note, it didn't show up in my initial search for;

read write configuration file c#

but I did find it when I searched for

read write config c#

Cheers.

Very helpful. I think msdn was written by cyborgs

I have an app.config------
<?xml version="1.0" encoding="utf-8" ?>




connectionString="Data Source=localhost\sqlexpress;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />

I have a winform which will take input from user to specify SQL ip address where I want to put code to write ConnectionString to the app.config file. how to do this?

"I think msdn was written by cyborgs"

What they lack in quality useful documentation they make up in volume and confusion.

Before you say something negative about MSDN - check out Java first!
Java documentation is as minimal as it can be, and any information a little more than class interface, can only be found on forums. And you are lucky if the answers are there.

@anon about Java

Everytime i run the application the changes done to app.config by the last run are not accessible.
How can i make the changes permanent so the the last run of program modification can be seen in next run

I'm not too sure about that java statement. It may be minimal, but at least the documentation is correct.

Java documentation is not minimal. It is concise.

How long were you working with java at a what level of complexity? Try to do something new and then tell me, and you have the combination of java with the different application servers... there's no information about the extrange errors that throw... you are lucky if after many hours find somethig usefull

Note that under Vista/Win7, your program may not be able to write the app.config settings back, as the file is stored next to your .exe (in Program Files). The user will require admin privileges on the PC in order to write back such changes.

True dat.msdn was written cyborgs.Thanks for this article.

Suggesting something (msdn) shouldn't be criticized because something else is worse (java) is ridiculous. The goal isn't to be the best of the bad.

Was so hoping this would work, but while it doesn't throw any exceptions, the app.config remains the same.

it does work - as someone noted, it might not work if user doesn't have permissions for write to the file.

And when you start from the visual studio, the \bin\debug\myproj.vshost.exe.config gets modified (or \bin\debug\myproj.exe.config, depends how you debug) - the app.config in solution will remain the same.

Thanks for the snippet. I created a method:

static void setConfigSetting(string setting, string newValue)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings[setting].Value = newValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}

But when I step through my code and execute the method, I do not see the change in any of the config files (App.config, .exe.config, or .vshost.exe.config).

But when I step through this code (that getConfigSetting is a local method that supplies a default - it just calls

ConfigurationManager.AppSettings.Get(setting)

as you would expect.

setConfigSetting("dailySuccess", "false");
Boolean DEBUGdailySuccess = true;
DEBUGdailySuccess = Boolean.Parse(getConfigSetting("dailySuccess", "true"));

I do in fact see DEBUGdailySuccess with a value of false! At what point will I see this reflected in the config file?

Yours in confusion!
dc

dedicated server hosting
Advertise on this site

Recent Comments