Changing data in App.config

  • Thread starter Thread starter Anatoly
  • Start date Start date
A

Anatoly

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
 
You cannot, well, unless you want to open the file into a DOM, update it,
then write it back out.
 
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.
 
Back
Top