System.Configuration Does Not Save

I am trying to use the Configuration object provided with the .NET 2.0
framework. I see that when I call the Save function it does update the
<application>.exe.config file. However, when I close the application and
check the <application>.exe.config file again, it has reverted back to how
it was when the application was started.

Is there something I am doing or not doing Here is part of my code: (And
before anyone writes it, I know I can use the DatabaseConfiguration object,
but this is simply for learning this object at this point.)

Thanks!

My Config Class:

Public Class FileGenClass : Inherits ConfigurationSection

<ConfigurationProperty("DatabaseServer")> _
Public Property DatabaseServer() As String
Get
Return Me.Item("DatabaseServer")
End Get
Set(ByVal value As String)
Me.Item("DatabaseServer") = value
End Set
End Property

<ConfigurationProperty("DatabaseName")> _
Public Property DatabaseName() As String
Get
Return Me.Item("DatabaseName")
End Get
Set(ByVal value As String)
Me.Item("DatabaseName") = value
End Set
End Property

<ConfigurationProperty("DatabaseUser")> _
Public Property DatabaseUser() As String
Get
Return Me.Item("DatabaseUser")
End Get
Set(ByVal value As String)
Me.Item("DatabaseUser") = value
End Set
End Property

<ConfigurationProperty("DatabasePassword")> _
Public Property DatabasePassword() As String
Get
Return Me.Item("DatabasePassword")
End Get
Set(ByVal value As String)
Me.Item("DatabasePassword") = value
End Set
End Property
...

Saving Code:

Try
moFileGen =
CType(ConfigurationManager.GetSection("DatabaseSettings"), FileGenClass)

If moFileGen Is Nothing Then
moFileGen = New FileGenClass
Dim oSettings As New Settings
oSettings.btnCancel.Enabled = False
If oSettings.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
moFileGen.DatabaseServer = oSettings.txtDBServer.Text
moFileGen.DatabaseName = oSettings.txtDBName.Text
moFileGen.DatabaseUser = oSettings.txtUser.Text
moFileGen.DatabasePassword = oSettings.txtPassword.Text

Dim oConfig As Configuration = _ ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
oConfig.Sections.Add("DatabaseSettings", moFileGen)
oConfig.Save()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try


Answer this question

System.Configuration Does Not Save

  • CI2

    Correct. You can verify this yourself pretty easily by executing your program through the Windows shell and not though Visual Studio, having it save it’s settings, closing and then rerunning it again in the same way.

  • Dave Poole

    Thanks for that information Brendan.

    So if I were to run this application as an end user would, my settings would be saved

  • Bontje

    Every time you build or run your application in Visual Studio the App.config file contained within your project is copied to your build directory and overwrites the file you had previously saved your settings to (programname.exe.config).

    Would manually copying your settings to the App.config file work for you so that you always start off with them



  • System.Configuration Does Not Save