Link to home
Start Free TrialLog in
Avatar of ipaman
ipaman

asked on

app.exe.config file writing

I would like to know a relatively painless way of reading and writng to a config file.
I am currently reading it via:

NameValueCollection settings=(NameValueCollection)ConfigurationSettings.AppSettings;
string value = settings["keyName"];

Is there a way to setValue("NewValue") on a specified key? If not, is there code
somewhere (other than The Code Project) that does this?

Thanks,
Ipaman
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

There is no direct method of writing in this file but ... don't forget that this is nothing more then an XML file!

See my column on that topic: http://www.utmag.com/May2003/Page59.asp
Avatar of mail2dolly
mail2dolly

Hi,

Reading and writing to config files is pretty simple.

use the property System.Configuration.ConfigurationSettings.AppSettings["YOUR_KEY_NAME"] to get/set values.

HTH,
Dolly
mail2dolly, have you tried setting a value with this syntax?
Yes, I have been using this extensively in my C# code to read from and write to config files.

Dolly
I'm sure you cannot.

From the help file:
ConfigurationSettings.AppSettings Property
Gets configuration settings in the <appSettings> Element configuration section.
[Visual Basic]
Public Shared ReadOnly Property AppSettings As NameValueCollection
[C#]
public static NameValueCollection AppSettings {get;}


As you can see, there is only a "get" into the definition.
I am sorry, that is true. You can only read values from config file. To write values, I guess you will have to use the XML file writing API.

You are right, its a small pain.

Then again, the idea of config files is generally not to change values programmatically. Those values are usually a part of the  program itself.

Sorry again about the get/set!
Avatar of ipaman

ASKER

I am using some code I found on 'The Code Project.com'. What it does is
it creates a XmlDocument with the same format as the app.config (or App.exe.config) writes to the document and replaces the App.exe.config file.
This is all fine and dandy during runtime, but when the app closes and re-opens, it is re-initialized with the original app.config settings thus losing all changes made during runtime.
Not sue how to get around that problem.
ipaman.
ASKER CERTIFIED SOLUTION
Avatar of vinhnl
vinhnl

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipaman

ASKER

vinhnl,
sounds like what I need! Can you provde a link or send the source code?

Thanks,
Ipaman.