Sure, but you may want to explore other options as well. I just happen to like this way, it doesn't mean there aren't better ways to do it.
Serialization is a big subect, in this case we're persisting an object to a file, then deserializing the file back into an object.
Here's my properties class:
VB Code:
Public Class SleuthNCICServiceProperties
' The server's address.
Public serverAddress As String
' The server's port.
Public serverPort As Integer
' The listening port.
Public listenPort As Integer
' Lots of decisions in the server application
' are based upon the stateUS. i.e. NM, CO, etc.
Public stateUS As String
Public sendInitialRegisterString As Boolean
Public initialRegisterORI As String
Public sendHeartbeat As Boolean
Public heartbeatInterval As Integer
Public heartbeatCharacter As Integer
End Class
This is how I dim my object in a different class:
VB Code:
Public Class SleuthNCICService
Inherits System.ServiceProcess.ServiceBase
' Configuration properties.
Public Shared properties As New SleuthNCICServiceProperties
.
.
.
I do this at the start of the application. It's an example of serialization in both directions:
VB Code:
Private Sub ReadProperties()
Dim fileName As String = Application.StartupPath & "\Sleuth-NCIC-Service-properties.xml"
Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(SleuthNCICServiceProperties))
'A first time thing only - if the properties file does not
'exist, create a default one.
If Not File.Exists(fileName) Then
properties.serverAddress = "localhost"
properties.serverPort = 4243
properties.listenPort = 4242
properties.stateUS = "NM"
properties.sendInitialRegisterString = True
properties.initialRegisterORI = "NM1234567"
properties.sendHeartbeat = True
properties.heartbeatInterval = 60000
properties.heartbeatCharacter = 5
Try
Dim myWriter As StreamWriter = New StreamWriter(fileName)
mySerializer.Serialize(myWriter, properties)
myWriter.Close()
Catch ex As Exception
Logger.Log("E", Encoding.ASCII.GetBytes(ex.ToString))
End Try
End If
'Deserialize the xml file into a properties object
Try
Dim myFileStream As FileStream = New FileStream(fileName, FileMode.Open)
properties = CType(mySerializer.Deserialize(myFileStream), SleuthNCICServiceProperties)
myFileStream.Close()
Catch ex As Exception
Logger.Log("E", Encoding.ASCII.GetBytes(ex.ToString))
End Try
End Sub ' ReadProperties()
The xml file looks like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<SleuthNCICServiceProperties xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<serverAddress>127.0.0.1</serverAddress>
<serverPort>4243</serverPort>
<listenPort>4242</listenPort>
<stateUS>NM</stateUS>
<sendInitialRegisterString>true</sendInitialRegisterString>
<initialRegisterORI>NM0260701</initialRegisterORI>
<sendHeartbeat>true</sendHeartbeat>
<heartbeatInterval>60000</heartbeatInterval>
<heartbeatCharacter>5</heartbeatCharacter>
</SleuthNCICServiceProperties>
Then in your code, you use the class members:
VB Code:
ReadProperties()
If properties.sendHeartbeat Then
TimerKeepAlive.Interval = properties.heartbeatInterval
TimerKeepAlive.Enabled = True
End If