locked
Update App.config at runtime? RRS feed

  • 問題

  • Hi

    I am wondering if it is possible to update the application configuration file at run time?

    I tried the following code on my VB application, it seems does not work.
     ConfigurationManager.AppSettings.Set("ExampleKey", "YEAH")
    and
    ConfigurationManager.AppSettings.Item("ExampleKey") = "YEAH"

    Could you give me some advice, how could I update the value on the app.config file at runtime.

    Many thanks
    2008年7月11日 10:11

解答

  • Hi, ChiYau,

     

    I found a reference about it form web, here is the URL: http://netcode.ru/dotnet/?lang=&katID=30&skatID=264&artID=7235.  You may expereince the "read-only" exceptional, but this artical proivde the way on solving it.  It is in C# but I think the concept is same.

     

    Hope it can help.  Smile

     

    2008年7月29日 13:20
  •  

    Hi ChiYau,

     

    if you will update the config, why don't you put it in the application settings?

    seems it's more suitable for your case.

     

    My.Settings.{YourItems} = value

    My.Settings.Save()

     

    Jacky

    2008年9月24日 2:05

所有回覆

  • Hi, ChiYau,

     

    I found a reference about it form web, here is the URL: http://netcode.ru/dotnet/?lang=&katID=30&skatID=264&artID=7235.  You may expereince the "read-only" exceptional, but this artical proivde the way on solving it.  It is in C# but I think the concept is same.

     

    Hope it can help.  Smile

     

    2008年7月29日 13:20
  • Hi Andy

    I have tried to follow the article, and converted the C Sharp code into VB. Unfortunately, it does not work = (. The code
    xmlDoc.SelectSingleNode("configuration/appSettings")  return nothing)

    Do you think it is because the version of the framework is different? The article is using .net 1.1 and I am using 2.0

    Could you give me some advice on where do I did it wrong.


       Private Sub AddKey(ByVal KeyName As String, ByVal KeyValue As String)
            Dim xmlDoc As New XmlDocument
            Dim AppSettingsNode As XmlNode = xmlDoc.SelectSingleNode("configuration/appSettings") //<-- It returns nothing

            Try

                Dim newChild As XmlNode = AppSettingsNode.FirstChild.Clone
                newChild.Attributes("key").Value = KeyName
                newChild.Attributes("value").Value = KeyValue
                xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory & "..\\..\\App.config")
                xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
                MsgBox("done")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

    Thanks in advance.


    2008年8月1日 8:47
  •  

    Hi ChiYau,

     

    if you will update the config, why don't you put it in the application settings?

    seems it's more suitable for your case.

     

    My.Settings.{YourItems} = value

    My.Settings.Save()

     

    Jacky

    2008年9月24日 2:05
  • Hi Jacky

    Thanks for your reply, It works very well.


    Thanks a lot
    2008年9月24日 7:50