在my.settings

时间:2016-08-21 15:59:10

标签: vb.net

我正在为儿童开发网络浏览器,

我需要在设置中存储网站列表,以便父母可以在网址列表中添加和删除网站,包括自定义网站。我该怎么做?

截图

the menu I want to add websites to

The add site tab of control panel

the remove site tab of control panel

1 个答案:

答案 0 :(得分:0)

我会在这里使用序列化,而不是将其存储在设置中。

您可以像这样创建一个WebsiteObject类:

<Serializable()>
Public Class WebsiteObject
Private name As string
Private url As String

Public Sub New()
    ' Leave fields empty. 
End Sub

Public Sub New(ByVal webname As DataSet, ByVal theurl As String)
    name = webname
    url = theurl

End Sub

Public Property WebsiteName as string
    Get
        Return name
    End Get
    Set(ByVal value As string)
        name = value
    End Set
End Property

Public Property WebsiteURL As String
    Get
        Return url
    End Get
    Set(ByVal value As String)
        url = value
    End Set
End Property
End Class

然后使用以下功能序列化和反序列化您的网站列表:

Public Sub WriteToBinaryFile(serializationFile As String, List As List(Of WebsiteObject))
    Using stream As Stream = File.Open(serializationFile, FileMode.Create)
        Dim bformatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        bformatter.Serialize(stream, List)
    End Using
End Sub


Public Function ReadFromBinaryFile(serializationFile As String)
    Using stream As Stream = File.Open(serializationFile, FileMode.Open)
        Dim bformatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()

        Dim websites As List(Of WebsiteObject) = DirectCast(bformatter.Deserialize(stream), List(Of FavoritesObject))
        Return websites
    End Using
End Function

要导入和导出您的网站列表,只需使用:

dim websitelist as new list(of WebsiteObject)
dim site as new WebsiteObject
site.WebsiteURL = "url here"
site.WebsiteName = "site name here"
websitelist.add(site)
'write website list to file
WriteToBinaryFile("filepathhere.bin", websitelist)
'read website list from file
dim newwebsitelist as list(of WebsiteObject)= ReadFromBinaryFile("samefilepathhere.bin")`