一种简便实效的配置文件读写方法

类别:.NET开发 点击:0 评论:0 推荐:

一种简便实效的配置文件读写方法

 

声明:我的“文章”只适宜初学者参考。我对概念理解模糊,参考者只参考思路则可。

(怕挨骂,声明在前)

 

主要做法:

第一步,将配置信息做一个简单的类。

要点:因为要用到序列化,而且是用XmlSerializer来序列化,所以要存到文件的变量信息都要声明为Public,要序列化的类用<Serializable()>修饰。

示例:一个连接数据库的配置信息

 

'配置信息

<Serializable()> Public Class Item

    '初始默认值

    Public ID As Integer = 0

    Public Server As String = "(Local)"

    Public Database As String = "NorthWind"

    Public Tusted As Boolean = True

    Public User As String = ""

    Public Password As String = ""

End Class

 

第二步:配置处理的类。

说明:这个类是通用的。用到别的配置,你只需修改上面的配置信息类就可以了。它实现了存档和读取的功能,并转化为配置信息对象,操作起来较为方便。你可以添加内容加密的处理。

 

<Serializable()> Public Class Config

    '默认ConnectionInfoID

    Public ID As Integer

    '必须是Public,XmlSerializer只对Pulbic变量作用

    Public Items As Item()

    Private m_Count As Integer = 0

 

    '添加配置信息

    Public Sub Add(ByVal ConnectionInfo As Item)

        If ConnectionInfo Is Nothing Then Exit Sub

        If m_Count > 0 Then

            If Not Me.ConnectionInfo(ConnectionInfo.ID) Is Nothing Then

                MsgBox(ID.ToString & "已存在")

                Exit Sub

            End If

        End If

        ReDim Preserve Me.Items(m_Count)

        Me.Items(m_Count) = ConnectionInfo

        m_Count += 1

    End Sub

    '取指定ID的配置信息

    Public ReadOnly Property ConnectionInfo(ByVal ID As Integer) As Item

        Get

            Dim mItem As Item

            Dim i As Integer

            For i = 0 To m_Count - 1

                If Me.Items(i).ID = ID Then

                    mItem = Me.Items(ID)

                    Exit For

                End If

            Next

            Return mItem

        End Get

    End Property

    '序列化并保存到指定文件

    Public Sub SaveXmlFile(ByVal FileName As String)

        If Me Is Nothing Then Exit Sub

        Dim XmlWriter As New System.IO.StreamWriter(FileName, False)

        XmlWriter.Write(SerializeClass.GetXML(Me))

        XmlWriter.Close()

    End Sub

    '从指定文件读取反序列化转为对象

    Public Function LoadXmlFile(ByVal FileName As String) As Config

        Dim XmlReader As New System.IO.StreamReader(FileName, System.Text.Encoding.Default)

        Dim mConfig As Config

        mConfig = CType(SerializeClass.LoadXML(GetType(Config), XmlReader.ReadToEnd), Config)

        XmlReader.Close()

        mConfig.m_Count = mConfig.Items.Length

        Return mConfig

    End Function

End Class

 

 

就以上两步就可以了,下面这个类是用来序列化用的。

 

'这是用XmlSerializer序列化对象的类

Public Class SerializeClass

    Public Shared Function GetXML(ByVal mObject As Object) As String

        Dim oSerializer As New System.Xml.Serialization.XmlSerializer(mObject.GetType)

        Dim oStringWriter As New System.IO.StringWriter

        oSerializer.Serialize(oStringWriter, mObject)

        Return oStringWriter.ToString

    End Function

    Public Shared Function LoadXML(ByVal mClass As Type, ByVal XML As String) As Object

        Dim oSerializer As New System.Xml.Serialization.XmlSerializer(mClass)

        Dim oStringReader As New System.IO.StringReader(XML)

        Return oSerializer.Deserialize(oStringReader)

    End Function

End Class

 

 

以下是使用方法示例:

 

    '测试存配置文件

    Private Sub TestSaveConfig()

        Dim mConfig As Config = New Config

        Dim mItem As Item

        mItem = New Item

        mConfig.Add(mItem)

 

        mItem = New Item

        With mItem

            .ID = 1

            .User = "Master"

            .Password = "123456"

            .Tusted = False

        End With

        mConfig.Add(mItem)

 

        mConfig.SaveXmlFile(Application.StartupPath & "\Config.xml")

End Sub

 

    '测试读配置文件

    Private Sub TestLoadConfig()

        Dim mConfig As Config = New Config

        mConfig = mConfig.LoadXmlFile(Application.StartupPath & "\Config.xml")

        '读取默认值的用户名

        MsgBox(mConfig.Items(mConfig.ID).User)

        '不妨修改一下配置

        mConfig.ID = 1

        mConfig.ConnectionInfo(0).Server = "Server"

        mConfig.ConnectionInfo(1).Database = "Master"

        '再存盘

        mConfig.SaveXmlFile(Application.StartupPath & "\Config.xml")

    End Sub

本文地址:http://com.8s8s.com/it/it43121.htm