By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

Writing to App.Config

P: n/a
ConfigurationSettings.AppSettings allows me to read from my config but how
do I write to my config file?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
_
Frank,
ConfigurationSettings.AppSettings allows me to read from my config but how
do I write to my config file?


Download the TFTP Server eval components from www.abderaware.com and look at the
Configuration class (configuration.cs).

Cya! :-)
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 15 '05 #2

P: n/a
Hi Frank,

Writting into .config file is not a good idea.
The config files are intended to give administrators a way to configure
certain settings and not to persist settings.
They reside (normally) under program files folder thus only admin (if not
set otherwise) can edit them.

If you have settings that you need to persist consider storing them in
user-specific or app-specific area.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
ConfigurationSettings.AppSettings allows me to read from my config but how
do I write to my config file?

Nov 15 '05 #3

P: n/a
_
On Tue, 10 Feb 2004 08:41:50 +0100, "Miha Markic [MVP C#]" <miha at rthand com>
wrote:
The config files are intended to give administrators a way to configure
certain settings and not to persist settings.
They reside (normally) under program files folder thus only admin (if not
set otherwise) can edit them.


What supporting documentation do you have for that statement? I don't mean to
be confrontational, I really am curious to know if I missed some position taken
by MS on that point.

--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 15 '05 #4

P: n/a
Hi Zane,
What supporting documentation do you have for that statement? I don't mean to be confrontational, I really am curious to know if I missed some position taken by MS on that point.


For instance, config files are placed into (under) program files folder
where only admins have write access by default.
It is generally not a good idea to allow every user to write into program
files folder.
That is also why MS doesn't support writting to config files.
For that purposes there are other folders (for example ApplicationData,
CommonApplicationData, LocalApplicationData) - some of them support also
roaming - you can find them by looking at
Environment.SpecialFolder Enumeration .net help topic.

There you'll find also a useful link with more details on special folders:
http://msdn.microsoft.com/library/de...nums/csidl.asp

Sorry, I don't have other better links right now, but AFAIK this is how it
should be.

HTH,
--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Nov 15 '05 #5

P: n/a
_
Miha,
For instance, config files are placed into (under) program files folder
where only admins have write access by default.
Whether Frank is developing for such a situation is something we don't know.

That is also why MS doesn't support writting to config files.
For that purposes there are other folders (for example ApplicationData,
CommonApplicationData, LocalApplicationData) - some of them support also
roaming - you can find them by looking at
Environment.SpecialFolder Enumeration .net help topic.
Or, one can simply use a configuration file in the application's directory.
From MSDN:

<q>
The name and location of the application configuration file depend on the
application's host, which can be one of the following:

Executable–hosted application.
The configuration file for an application hosted by the executable host is in
the same directory as the application. The name of the configuration file is the
name of the application with a .config extension. For example, an application
called myApp.exe can be associated with a configuration file called
myApp.exe.config.
</q>
ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconapplicationconfigurationfiles.htm

Sorry, I don't have other better links right now ...


Well, until you find some better links ... :-)
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 15 '05 #6

P: n/a
What about on installation? I have on several occasions had an installation
class write to the App.Config. I create a DataSet, then read the App.Config
with ReadXml() . The installer class then updates the Data Set and then
calls WriteXml method of the data set.
I would agree that you should avoid using .config files for most things, but
they seem a natural for things you would otherwise use the registry for.
Else why would they have the appSettings section?

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
Hi Frank,

Writting into .config file is not a good idea.
The config files are intended to give administrators a way to configure
certain settings and not to persist settings.
They reside (normally) under program files folder thus only admin (if not
set otherwise) can edit them.

If you have settings that you need to persist consider storing them in
user-specific or app-specific area.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
ConfigurationSettings.AppSettings allows me to read from my config but how do I write to my config file?


Nov 15 '05 #7

P: n/a
_
On Tue, 10 Feb 2004 11:58:18 -0700, "John Smith" <jb****@blackfoot.com> wrote:
Else why would they have the appSettings section?


Bingo! :-)
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 15 '05 #8

P: n/a

<zane (_) abderaware.com (Zane Thomas)> wrote in message
news:40***************@news.microsoft.com...
Miha,
For instance, config files are placed into (under) program files folder
where only admins have write access by default.
Whether Frank is developing for such a situation is something we don't

know.

Yes, we don't know. I was talking about common scenarios.
That is also why MS doesn't support writting to config files.
For that purposes there are other folders (for example ApplicationData,
CommonApplicationData, LocalApplicationData) - some of them support also
roaming - you can find them by looking at
Environment.SpecialFolder Enumeration .net help topic.
Or, one can simply use a configuration file in the application's

directory. From MSDN:

<q>
The name and location of the application configuration file depend on the
application's host, which can be one of the following:

Executable-hosted application.
The configuration file for an application hosted by the executable host is in the same directory as the application. The name of the configuration file is the name of the application with a .config extension. For example, an application called myApp.exe can be associated with a configuration file called
myApp.exe.config.
</q>
ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconapplicationconfigurationfiles.
htm

Configuration file - not persistent storage file.
Sorry, I don't have other better links right now ...


Well, until you find some better links ... :-)


Do I need them? ;-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Nov 15 '05 #9

P: n/a

"John Smith" <jb****@blackfoot.com> wrote in message
news:uc**************@TK2MSFTNGP09.phx.gbl...
What about on installation?
Yes, what about it?

I have on several occasions had an installation class write to the App.Config. I create a DataSet, then read the App.Config with ReadXml() . The installer class then updates the Data Set and then
calls WriteXml method of the data set.
I would agree that you should avoid using .config files for most things, but they seem a natural for things you would otherwise use the registry for.
Else why would they have the appSettings section?


Installation is done under administrator account and have privilege and
right to write into config.
It makes sense to create or modify config file at installation time.
However, they are not intended to be used for storing persistent data from
the application itself, like for example windows position - stuff like that.
Sorry if I was not clear enough on this.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Nov 15 '05 #10

P: n/a
_
Miha,
Configuration file - not persistent storage file.


It's far from uncommon for users to change configuration setttings so I fail to
see how reframing the issue as one of persistence is really relevant.
>Sorry, I don't have other better links right now ...


Well, until you find some better links ... :-)


Do I need them? ;-)


Sure looks that way from here. :-)
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 15 '05 #11

P: n/a
How do we determine what the user specific or app specific area is on any
given machine? Is there a method that we can call like Path.GetTempPath()?
On windows machines I could assume that C:\Documents and Settings\<user
name>\Application Data\ exists, but is it a safe assumption?. Will this
location always be present on the C drive? What about other operating
systems like the Macintosh? I'm not arguing here for using the config file
for this, I'm wondering how I can determine a safe spot for application
specific data.

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
Hi Frank,

Writting into .config file is not a good idea.
The config files are intended to give administrators a way to configure
certain settings and not to persist settings.
They reside (normally) under program files folder thus only admin (if not
set otherwise) can edit them.

If you have settings that you need to persist consider storing them in
user-specific or app-specific area.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
ConfigurationSettings.AppSettings allows me to read from my config but how do I write to my config file?


Nov 15 '05 #12

P: n/a
Frank,
you might find my "proof of concept" class library to do this helpful. I
don't use it much, but it works:

http://www.eggheadcafe.com/articles/20030907.asp

Peter

"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
ConfigurationSettings.AppSettings allows me to read from my config but how
do I write to my config file?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #13

P: n/a
Hi John,

"John Smith" <jb****@blackfoot.com> wrote in message
news:uO****************@tk2msftngp13.phx.gbl...
How do we determine what the user specific or app specific area is on any
given machine? Is there a method that we can call like Path.GetTempPath()? On windows machines I could assume that C:\Documents and Settings\<user
name>\Application Data\ exists, but is it a safe assumption?.
Certainly not.
Use Environment.GetFolderPath method.

Will this location always be present on the C drive? What about other operating
systems like the Macintosh?
Macintosh? Is there a .net on Macintosh? ;-)

I'm not arguing here for using the config file for this, I'm wondering how I can determine a safe spot for application
specific data.


See the first answer.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
Nov 15 '05 #14

P: n/a
WTF?
Why the not write to an App.Config?
I can think of a hell of a lot of situations where it is critical to do so, for a real app..
Just off the top of my head, how about actually providing an app to allow the Admins to modify the settings for a Windows app to their specific installation? Server names, addresses, etc
Saying that it isn't supported and it's a bad idea isn't particularly useful, I don't recall anyone mentioning whether or not the original request was for admin use or not, comes to that..
The documentation on config seems a bit dicey to me, personally, and it's a reasonable question

Nov 16 '05 #15

P: n/a
WTF?
Why the not write to an App.Config?
I can think of a hell of a lot of situations where it is critical to do so, for a real app..
Just off the top of my head, how about actually providing an app to allow the Admins to modify the settings for a Windows app to their specific installation? Server names, addresses, etc
Saying that it isn't supported and it's a bad idea isn't particularly useful, I don't recall anyone mentioning whether or not the original request was for admin use or not, comes to that..
The documentation on config seems a bit dicey to me, personally, and it's a reasonable question

Nov 16 '05 #16

P: n/a
Hi,

"jc******@cox.net" <an*******@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
WTF??
Why the not write to an App.Config??
I can think of a hell of a lot of situations where it is critical to do so, for a real app... Just off the top of my head, how about actually providing an app to allow the Admins to modify the settings for a Windows app to their specific
installation? Server names, addresses, etc?

Yes, but only windows administrators have write access to program files
folder by default. It might make sense allow administrators to change the
app.config programatically but one really should not allow normal users to
write there.
Saying that it isn't supported and it's a bad idea isn't particularly useful, I don't recall anyone mentioning whether or not the original request
was for admin use or not, comes to that... The documentation on config seems a bit dicey to me, personally, and it's

a reasonable question.

There is no built in support. But you can edit the file as any xml file.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
Nov 16 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.