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("DatabaseSet tings"), 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(Configur ationUserLevel.None)
oConfig.Sections.Add("DatabaseSettings", moFileGen)
oConfig.Save()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
....