473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

.config file - writing to it?

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.
Oct 24 '05 #1
4 1547
CT
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" <fr**@nowhere.com> wrote in message
news:ex**************@TK2MSFTNGP10.phx.gbl...
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.

Oct 24 '05 #2
MUS
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.Co nfigurationFile;
// 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

Oct 25 '05 #3
UJ
Moiz,
Thanks for the info.

UJ.

"MUS" <mo********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.Co nfigurationFile;
// 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

Oct 25 '05 #4
> 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.LocalA pplicationData;

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

* 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.Appl icationData;

I hope this answers your question?
Oct 26 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Maxim Khesin | last post by:
I want to have a config file with my python proggie, satisfying the following requirements: 1) support key->(value, default) 2) simple and intuitive to read and edit 3) easyly readable into a...
3
by: Craig | last post by:
I've developed an single sign on web application that we'd like to use with all the applications we're developing. The application's connection string is stored in web.config. I've gotten some...
3
by: baylor | last post by:
I'm having a weird bug and wondering if anyone has ever seen this before A Hello World GUI app. Create an app.config. When you build, Studio copies app.config to bin\debug\ and renames it to the...
5
by: who be dat? | last post by:
Hello all. I'm writing an application that is writing trace information that can be viewed in trace.axd. I would like to rename this and use a different name specific to my application. I know...
4
by: dale | last post by:
I am trying store and update some variables/options that the user can set and that need to be saved and used each time the program is run. I am thinking that they should be stored in the...
4
by: feng | last post by:
..Net's .config file is supposed to replace the .ini files. That's all fine if all I need is to read from it. But what if I need both read and write? With .ini file, I can do that very easily with...
17
by: Fred Nelson | last post by:
Hi: I have written several web applications that obtain their connection strings from the web.config file. This is very easy to use and it makes it easy to move an app from development into...
9
by: WT | last post by:
Hello, I am writing a Web install project and I want to get the connection string from user and write it in the web.config of the installed web site. As the Websetup is a windows program, I use...
0
parshupooja
by: parshupooja | last post by:
Hey All, I have old vb.net application and I have various users for that.where I have XML file as datasource. Till now for each user application was installed seperately. only difference...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.