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=(NameValueCollect ion)Config urationSet tings.AppS ettings;
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
I am currently reading it via:
NameValueCollection settings=(NameValueCollect
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
Hi,
Reading and writing to config files is pretty simple.
use the property System.Configuration.Confi gurationSe ttings.App Settings[" YOUR_KEY_N AME"] to get/set values.
HTH,
Dolly
Reading and writing to config files is pretty simple.
use the property System.Configuration.Confi
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
Dolly
I'm sure you cannot.
From the help file:
ConfigurationSettings.AppS ettings 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.
From the help file:
ConfigurationSettings.AppS
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!
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!
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
vinhnl,
sounds like what I need! Can you provde a link or send the source code?
Thanks,
Ipaman.
sounds like what I need! Can you provde a link or send the source code?
Thanks,
Ipaman.
See my column on that topic: http://www.utmag.com/May2003/Page59.asp