PC Review


Reply
Thread Tools Rate Thread

.config file - writing to it?

 
 
UJ
Guest
Posts: n/a
 
      24th Oct 2005
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.


 
Reply With Quote
 
 
 
 
CT
Guest
Posts: n/a
 
      24th Oct 2005
Jeff,

You can either create your own class for reading and writing the config
file, or use the Configuration Application Block,
http://msdn.microsoft.com/library/de...tml/config.asp

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"UJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
MUS
Guest
Posts: n/a
 
      25th Oct 2005
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

 
Reply With Quote
 
UJ
Guest
Posts: n/a
 
      25th Oct 2005
Moiz,
Thanks for the info.

UJ.

"MUS" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Olaf Baeyens
Guest
Posts: n/a
 
      26th Oct 2005
> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing to config file =?Utf-8?B?Vmlua2k=?= Microsoft ASP .NET 3 21st Nov 2007 03:45 PM
Writing out to a .config file. Mufasa Microsoft C# .NET 2 3rd May 2007 01:30 PM
Writing to the APP.CONFIG file John Cosmas Microsoft Dot NET Framework 3 23rd Jan 2006 04:44 PM
Writing value to config file =?Utf-8?B?Sm9iIExvdA==?= Microsoft Dot NET 2 24th Dec 2004 09:29 AM
writing config-file MK Microsoft C# .NET 2 24th Jul 2003 03:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:33 AM.