.config file - writing to it?

  • Thread starter Thread starter UJ
  • Start date Start date
U

UJ

Is there a way to write into the .config file? In other words treat it like
a .ini file. (I may be confused on what the exact nature of the .config file
may be.)

I can read from it fine but would also like to be able to write to it.

TIA - Jeff.
 
Hello UJ!

You ".config" indeed is an xml file even when its been given an
extension of ".config". So what you can do is to load the ".config"
file like any regular xml file like this:

// This statement will get you your config file
String fileName =
System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
// Load the config file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);

Since now you have got yourself DOM of the config file you can operate
on the in-memory DOM using:

XmlElement element = xmlDoc.CreateElement(<YOUR_ELEMENT>);

and also by use of the following methods:

1) SelectSingleNode()
2) AppendChild()
3) RemoveChild()
4) ImportNode()

and there you go possibilities are unlimited.

Note: But you need to remember onething that config file are
"read-once-cache-forever" story i.e. app.config file will be read as
your application starts and even if you write anything to it change
wont get reflected untill you restart your application.

If you are concerned about a set of custom elements in your config
file, may be something like this:

<DatabaseRepository>
<DataSources>
<DataSource name="sqlserver" connection-string="sql-conn-str" />
<DataSource name="oracle" connection-string="oracle-conn-str" />
</DataSources>
</DatabaseRepository>

then you can modify the scheme as:

<DatabaseRepository file-ref="dbrep.xml" />

where your "dbrep.xml" file will be a seperate file linked with your
app.config and then you can read from it and write to it at runtime
without any problem.

I hope this might be of some help.

Let me know in case of any inconsistancy.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd
 
Is there a way to write into the .config file? In other words treat it
like
a .ini file. (I may be confused on what the exact nature of the .config file
may be.)
The config file is used for starting up conditions and custom changes of the
program without need of recompiling.
For example, you could configure differen splash screens.
Normally this file is not supposed to be written to, it is only intended for
read-only purposes.
The reason for this is that this file might be on e CDROM ro DVD of
protected program files folder.

To save configuration settings, you have to create your own implementation
and should be store in any of these locations "Documents and Settings" which
should be read-write enabled. Not the old style "Program files" folder.

You have 3 locations:
* Locally for your won login account
m_LocalUserConfig=Environment.SpecialFolder.LocalApplicationData;

* Globally for all users on this machine
m_AllUserConfig=Environment.SpecialFolder.CommonApplicationData;

* Roaming profile that keeps the settings if you move to another computer
you get the last saved settings from another computer.
m_RoamingUserConfig=Environment.SpecialFolder.ApplicationData;

I hope this answers your question?
 
Back
Top