Write to XML configuration File in C#

JoeCDaMan

Senior member
Sep 17, 2001
211
0
0
I want to write to an XML configuration file but I get an error message saying the following:

code: System.Configuration.ConfigurationSettings.AppSettings("compNameConf") = "some string value"

message: "An unhandled exception of type 'System.NotSupportedException' occurred in system.dll
Additional information: Collection is read-only."

I am reading the file ok, with this code:

string str;

str = System.Configuration.ConfigurationSettings.AppSettings("compNameConf")

How do I alleviate this problem?

Thanks
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Well, the configuration file is Read-Only... they simply don't allow you to modify it. You could get around this by opening the file directly and then use XML reader/writer to make changes.

So since you gotta do all that work I just abandoned that file and created a simple INI file in IsolatedStorage to store my app's settings which get updated every once in a while
 

JoeCDaMan

Senior member
Sep 17, 2001
211
0
0
thanks Iozina, yea I think I will use the xml writer to solve the problem, you need to have Administrative rights to install and run my app anyway so it doesn't matter.

For anyone else who wants to know why the app.config file is read only, I just found this article that explains it a little bit more.

http://www.interact-sw.co.uk/i...004/11/25/savingconfig

 

torpid

Lifer
Sep 14, 2003
11,631
11
76
if this is a web app then you should not do this unless you don't mind the app restarting every time you make the change. if it's a forms app it should be ok.
 

JoeCDaMan

Senior member
Sep 17, 2001
211
0
0
if this is a web app then you should not do this unless you don't mind the app restarting every time you make the change. if it's a forms app it should be ok.

Technically you don't need to restart the web app, you just need to refresh the cache and pick up any changes you made to the .config
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
Originally posted by: JoeCDaMan
if this is a web app then you should not do this unless you don't mind the app restarting every time you make the change. if it's a forms app it should be ok.

Technically you don't need to restart the web app, you just need to refresh the cache and pick up any changes you made to the .config

I don't think you are following. If you alter the web.config file in a web application, it will restart with or without your permission.