469,306 Members | 1,987 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Changing data in App.config

I'm trying to use VB.NET's 2003 ConfigurationSettings.AppSettings.Set()
method of updating values but getting error "Collection is read-only".

How can I change values of keys in the App.config file programatically?

Here is my code:
Imports System.Configuration

Imports System.Collections.Specialized

Module Module1

Sub Main()

Dim sAttr As String

sAttr = ConfigurationSettings.AppSettings("Key0")

MessageBox.Show(sAttr)

ConfigurationSettings.AppSettings.Set("Key0", "11")

MessageBox.Show(sAttr)

End Sub

End Module

'***********************************************

'App Config File:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="Key0" value="00"/>

</appSettings>

</configuration>

'********************************************

-Anatoly
Jul 22 '05 #1
2 9365
You cannot, well, unless you want to open the file into a DOM, update it,
then write it back out.

"Anatoly" <nospam.com> wrote in message
news:Ol**************@TK2MSFTNGP15.phx.gbl...
I'm trying to use VB.NET's 2003 ConfigurationSettings.AppSettings.Set()
method of updating values but getting error "Collection is read-only".

How can I change values of keys in the App.config file programatically?

Here is my code:
Imports System.Configuration

Imports System.Collections.Specialized

Module Module1

Sub Main()

Dim sAttr As String

sAttr = ConfigurationSettings.AppSettings("Key0")

MessageBox.Show(sAttr)

ConfigurationSettings.AppSettings.Set("Key0", "11")

MessageBox.Show(sAttr)

End Sub

End Module

'***********************************************

'App Config File:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="Key0" value="00"/>

</appSettings>

</configuration>

'********************************************

-Anatoly

Jul 22 '05 #2
See this blog for full source code and REAL cautions.
http://ryanfarley.com/blog/archive/2004/07/13/879.aspx

Note, if your app will EVER be used by more than one person on the same
machine, then writing to the app config means they will share the config.
Your app also must have admin privs, since the app config lives in the
program files folder. On machines where the desktop environment is
centrally managed, an idea whose time has come, your app won't run.

Instead, consider saving any local configuration changes in the user's
Documents and Settings folder.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Anatoly" <nospam.com> wrote in message
news:Ol**************@TK2MSFTNGP15.phx.gbl...
I'm trying to use VB.NET's 2003 ConfigurationSettings.AppSettings.Set()
method of updating values but getting error "Collection is read-only".

How can I change values of keys in the App.config file programatically?

Here is my code:
Imports System.Configuration

Imports System.Collections.Specialized

Module Module1

Sub Main()

Dim sAttr As String

sAttr = ConfigurationSettings.AppSettings("Key0")

MessageBox.Show(sAttr)

ConfigurationSettings.AppSettings.Set("Key0", "11")

MessageBox.Show(sAttr)

End Sub

End Module

'***********************************************

'App Config File:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="Key0" value="00"/>

</appSettings>

</configuration>

'********************************************

-Anatoly

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Anatoly | last post: by
1 post views Thread by Bala Nagarajan | last post: by
5 posts views Thread by John | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.