A simple and effective configuration file reading method

xiaoxiao2021-03-19  187

The first step is to make the configuration information a simple class.

Key points: because of serialization,

XMLSerializer

To serialize, the variable information to be saved to the file must be declared as public, and sequentially use modification.

Example: A configuration information for connecting the database

'

Configuration information

public class item

'

Initial default value

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

Step 2: Configure the class.

Description: This class is universal. With other configurations, you only need to modify the above configuration information classes. It implements the function of archiving and reading, and translates into configuration information objects, which is more convenient to operate.

You can add content encryption.

public class config

'

Default ConnectionInfo ID

Public ID as integer

'

Must be public, XMLSerializer only for Pulbic variables

Public items as item ()

PRIVATE M_COUNT AS INTEGER = 0

'

Add configuration information

Public Sub Add (ByVal ConnectionInfo As Item)

IF connectioninfo is nothing the eXIT SUB

IF m_count> 0 THEN

IF not me.connectioninfo (connectioninfo.id) is nothing then

Msgbox (id.tostring & "

existed")

EXIT SUB

END IF

END IF

Redim preserve me.Items (m_count)

Me.Items (m_count) = ConnectionInfo

m_count = 1

End Sub

'

Take the configuration information of the specified 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

'

Serialized and saved to the specified file

Public Sub Savexmlfile (Byval FileName As String)

IF me is nothing kil

DIM XMLWRITER AS New System.io.Streamwriter (FileName, False)

XMLWRITER.WRITE (SERIALIZECLASS.GETXML ​​(ME))

XMLWriter.Close ()

End Sub

'

Reading the reverse selecente from the specified file to the object

Public Function LoadXmlFile (Byval FileName As String) AS Configdim 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

The above two steps can be, and this class is used to serialize.

'

This is the class with XMLSerializer serialized objects.

Public

Class SerializationClass

Public Shared Function GetXML (Byval Mobject As Object) AS STRING

Dim Osterializer As New System.xml.Serialization.xmlSerializer (MOBJECT.GETTYPE)

Dim OstringWriter as new system.io.stringwriter

Osterializer.Serialize (OstringWriter, MOBJECT)

Return OstringWriter.toString

END FUNCTION

Public Shared Function LoadXML (Byval McLass As Type, Byval XML AS String) AS Object

Dim Osterializer As New System.xml.Serialization.xmlSerializer (McLAS)

Dim OstringReader As New System.io.StringReader (XML)

Return Osterializer.deSerialize (OstringReader)

END FUNCTION

End

Class

The following is an example of using:

'

Test storage configuration file

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

'

Test read configuration file

Private sub testloadConfig ()

DIM MCONFIG AS config = new config

Mconfig = Mconfig.LoadXmlfile (Application.Startuppath & "/ Config.xml")

'

Read the username of the default value

MsgBox (Mconfig.Items (Mconfig.ID) .user)

'

Must confirm the configuration

Mconfig.ID = 1

Mconfig.connectionInfo (0) .server = "server" mconfig.connectionInfo (1) .Database = "master"

'

Repository

Mconfig.savexmlfile (Application.Startuppath & "/Config.xml")

End Sub

转载请注明原文地址:https://www.9cbs.com/read-130250.html

New Post(0)