Overwirting a value in the app.config

VB.NET 2005

 <connectionStrings>
    <add name="Test.My.MySettings.ConnectrionString" connectionString="Data Source=(local);Initial Catalog=;User ID=;Password="
      providerName="System.Data.SqlClient" />
  </connectionStrings>
 
I want to overwrite a connectionstring in the app.config fiel        

        Dim asm As Assembly = Assembly.GetExecutingAssembly
        Dim fi As FileInfo = New FileInfo(asm.Location + ".config")

        Dim sConnectionString As String = "Data Source=*;Initial Catalog=;User ID=;Password="
        Dim sNewConnectionString As String = sConnectionString.Replace("*", serverName)

        ' Loads the config file into the XML DOM.
        Dim doc As New System.Xml.XmlDataDocument
        doc.Load(fi.FullName)

        ' Finds the right node and change it to the new value.
        Dim node As XmlNode
        For Each node In doc.Item("configuration").Item("connectionStrings")
            If node.Name = "add" Then
>>> UNSURE WHAT TO DO HERE              

I used to use

       If Node.Name = "add" Then
                If Node.Attributes.GetNamedItem("key").Value = "cn" Then
                    Node.Attributes.GetNamedItem("value").Value = newCn


but these are no longer supported and the app.config file has changed


             End If
        Next node

        ' Write out the new config file.
        doc.Save(fi.FullName)
rocky050371Asked:
Who is Participating?
 
drichardsCommented:
There is an API for managing config files.  See the docs for ConfigurationManager:
http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

You can use the default app config (like in my sample code) of you can specify a specific file.  You can even encrypt sections (like the connectionStrings section if you put passwords in the config file).

Here is some code that modifies config values:

            Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfg.ConnectionStrings.ConnectionStrings["Test.My.MySettings.ConnectrionString"].ConnectionString = "Data Source=localhost;Initial Catalog=;User ID=me;Password=password";
            cfg.AppSettings.Settings.Add("newKey", "newValue");
            cfg.AppSettings.Settings.Remove("myOtherConfig");
            cfg.AppSettings.Settings["myConfig"].Value = "modifiedConfigValue";
            cfg.Save();


based on this config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="myConfig" value="myConfigValue"/>
    <add key="myOtherConfig" value="myOtherValue"/>
  </appSettings>
  <connectionStrings>
    <add name="Test.My.MySettings.ConnectrionString" connectionString="Data Source=(local);Initial Catalog=;User ID=;Password="
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
0
 
DhaestCommented:
Runtime Web.config / App.config Editing
http://www.eggheadcafe.com/articles/20030907.asp
0
 
drichardsCommented:
I suppose I should have put up VB code for this one...

        Dim cfg As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        cfg.ConnectionStrings.ConnectionStrings("Test.My.MySettings.ConnectrionString").ConnectionString = "Data Source=localhost;Initial Catalog=;User ID=me;Password=password"
        cfg.AppSettings.Settings.Add("newKey", "newValue")
        cfg.AppSettings.Settings.Remove("myOtherConfig")
        cfg.AppSettings.Settings("myConfig").Value = "modifiedConfigValue"
        cfg.Save()
0
 
rocky050371Author Commented:
It was fine, worked a treat
0
 
drichardsCommented:
Also, if you run this in Visual Studio, don't be surprised if the <project>.exe.config is not changed when you are finished.  In C# the environment leaves it changed on disk, but VB seems to replace it with the app.config source when the program is complete.  If you want to see the resulting config file, you have to run the exe outside of the Visual Studio environment.  And if you run it in the debugger, it uses the <project>.vshost.exe.config and not the <project>.exe.config.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.