473,396 Members | 1,879 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,396 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 1549
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.