我有一个 Visual Basic .Net 2.0 程序。我正在将设置从较旧的设置文件移至 app.config 程序设置文件。我正在努力尽可能地做好这件事。
所以,我添加了我的设置如图所示 https://i.stack.imgur.com/eL0aa.png.
加载时我这样做:
If My.Settings.databaseConnectionSettings Is Nothing Then
My.Settings.databaseConnectionSettings = New ArrayList()
End If
这是我的自定义类:
Imports System.Xml.Serialization
<Serializable()> Public Class DatabaseConnectionSettings
Private _nickname As String = String.Empty
Private _username As String = String.Empty
Private _password As String = String.Empty
Private _database As String = String.Empty
Private _server As String = String.Empty
Private _ssl As Boolean
Public Sub New()
_nickname = ""
_username = ""
_password = ""
_database = ""
_server = ""
_ssl = False
End Sub
Public Sub New(ByVal nickname As String, ByVal username As String, _
ByVal password As String, ByVal database As String, _
ByVal server As String, ByVal ssl As Boolean)
_nickname = nickname
_username = username
_password = password
_database = database
_server = server
_ssl = ssl
End Sub
Public Property nickname() As String
Get
Return _nickname
End Get
Set(ByVal Value As String)
_nickname = Value
End Set
End Property
Public Property username() As String
Get
Return _username
End Get
Set(ByVal Value As String)
_username = Value
End Set
End Property
Public Property password() As String
Get
Return _password
End Get
Set(ByVal Value As String)
_password = Value
End Set
End Property
Public Property database() As String
Get
Return _database
End Get
Set(ByVal Value As String)
_database = Value
End Set
End Property
Public Property server() As String
Get
Return _server
End Get
Set(ByVal Value As String)
_server = Value
End Set
End Property
<XmlElementAttribute(ElementName:="ssl")> Public Property ssl() As Boolean
Get
Return _ssl
End Get
Set(ByVal Value As Boolean)
_ssl = Value
End Set
End Property
End Class
而且,这就是我使用它的方式:
Dim databaseSettings As New DatabaseConnectionSettings( _
Me.txtNickName.Text, Me.txtUser.Text, Me.txtPass.Text, Me.txtData.Text, _
Me.txtServer.Text, Me.chkSSL.Checked)
'This statement will increment the arraylist count'
My.Settings.databaseConnectionSettings.Add(databaseSettings)
'This statement will save everything but the array list'
My.Settings.Save()
'This statement reloads everything, but the array list. The array list count after this goes to zero.'
My.Settings.Reload() 'If I remove this, program works fine until next run.'
所以,问题是,如何将 DatabaseConnectionSettings 的数组列表保存到持久存储中?我想以尽可能最干净的方式做到这一点。也就是说,我不想每次使用它时都必须将其转换为字符串或将其保存到单独的文件中。我希望能够使用 My.Settings 访问器方法。
请注意,它工作正常,只是没有保存到存储中。