Introduction
An alternative way to save/read settings from files is using serialization with just a few lines of code..
Background
There are many ways to keep settings. This sample is just an alternative way to save, load settings. These methods also can be used in CF with a few differences such as when reading a file..
Applications need settings such as web service URL, connectionstring, or user defined settings. During VB6 time we used ini files. But after .NET and Serialization technology came about,
there is no need to use ini files. Maybe application config files is the best solution for settings but this is just a usefull alternative.
Using the code
The main setting class looks like:
<Serializable()> _
Public Class Class_Settings
Implements System.IDisposable
#Region "Properties"
Private Cls_Setting1 As String = String.Empty
Property Setting1() As String
Get
Return Cls_Setting1
End Get
Set(ByVal value As String)
Cls_Setting1 = value
End Set
End Property
Private Cls_LongSetting As Long = -1
Property LongSetting() As Long
Get
Return Cls_LongSetting
End Get
Set(ByVal value As Long)
Cls_LongSetting = value
End Set
End Property
Private Cls_DoubleSetting As Double = 0.0
Property DoubleSetting() As Double
Get
Return Cls_DoubleSetting
End Get
Set(ByVal value As Double)
Cls_DoubleSetting = value
End Set
End Property
Private Cls_SubSetting As Class_SubSetting = Nothing
Property SubSetting() As Class_SubSetting
Get
Return Cls_SubSetting
End Get
Set(ByVal value As Class_SubSetting)
Cls_SubSetting = value
End Set
End Property
Private Cls_ListSubSetting As List(Of Class_SubSetting) = Nothing
Property ListSubSetting() As List(Of Class_SubSetting)
Get
Return Cls_ListSubSetting
End Get
Set(ByVal value As List(Of Class_SubSetting))
Cls_ListSubSetting = value
End Set
End Property
#End Region
#Region "New + Overrides"
Public Sub New()
End Sub
Public Overrides Function ToString() As String
Return ToXmlString()
End Function
#End Region
#Region "Read & Write From xml"
Private Function ToXmlString() As String
Try
Dim xmloutput As New IO.StringWriter
Dim xmls As String = String.Empty
Dim s As System.Xml.Serialization.XmlSerializer = _
New System.Xml.Serialization.XmlSerializer(Me.GetType())
s.Serialize(xmloutput, Me)
xmls = xmloutput.ToString
Return xmls
Catch ex As Exception
Return String.Empty
End Try
End Function
Private Function FromXmlString(ByVal _xml As String) As Class_Settings
Try
Dim xmloutput As New IO.StringReader(_xml)
Dim xmls As String = String.Empty
Dim s As System.Xml.Serialization.XmlSerializer = _
New System.Xml.Serialization.XmlSerializer(Me.GetType())
Dim obj As Class_Settings = CType(s.Deserialize(xmloutput), Class_Settings)
Return obj
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function FromXmlFile(ByVal _file As String) As Class_Settings
Try
If System.IO.File.Exists(_file) Then
Dim _xml As String = System.IO.File.ReadAllText(_file)
Return FromXmlString(_xml)
Else
Throw New Exception("File No Found")
End If
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function SaveToXmlFile(ByVal _filename As String) As Boolean
Try
Dim _fileinfo As New System.IO.FileInfo(_filename)
Dim _directory As String = _fileinfo.DirectoryName
If Not (System.IO.Directory.Exists(_directory)) Then
System.IO.Directory.CreateDirectory(_directory)
End If
System.IO.File.WriteAllText(_filename, ToXmlString)
Return True
Catch ex As Exception
Return False
End Try
End Function
#End Region
#Region "Dispose"
Private disposedValue As Boolean = False
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
End If
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
#End Region
End Class
The general idea is serialization in runtime and save the serialized XML string to file or read XML and load data from the XML file to class.
Using settings As New Class_Settings
settings.DoubleSetting = 1.1
settings.ListSubSetting = New List(Of Class_SubSetting)
For i As Int16 = 0 To 2
settings.ListSubSetting.Add(New Class_SubSetting(_
String.Format("Sub Setting - {0}", i + 1), i))
Next
settings.LongSetting = Now.Ticks
settings.Setting1 = "Setting1 value"
settings.SubSetting = New Class_SubSetting("Sub Setting", 0)
settings.SaveToXmlFile(String.Format("{0}\Settings\Settings.xml", Application.StartupPath))
End Using
XML output like:
="1.0"="utf-16"
<Class_Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Setting1>Setting1 value</Setting1>
<LongSetting>634732088762493969</LongSetting>
<DoubleSetting>1.1</DoubleSetting>
<SubSetting>
<SubSettingName>Sub Setting</SubSettingName>
<IntegerSetting>0</IntegerSetting>
</SubSetting>
<ListSubSetting>
<Class_SubSetting>
<SubSettingName>Sub Setting - 1</SubSettingName>
<IntegerSetting>0</IntegerSetting>
</Class_SubSetting>
<Class_SubSetting>
<SubSettingName>Sub Setting - 2</SubSettingName>
<IntegerSetting>1</IntegerSetting>
</Class_SubSetting>
<Class_SubSetting>
<SubSettingName>Sub Setting - 3</SubSettingName>
<IntegerSetting>2</IntegerSetting>
</Class_SubSetting>
</ListSubSetting>
</Class_Settings>
We have the XML file, let's deserialize and load the setting class from the XML file.
Dim _setting As New Class_Settings
_setting = _setting.FromXmlFile(String.Format("{0}\Settings\Settings.xml", _
Application.StartupPath))
For flexibility add propertygrid and bind setting class to it and give a control to user to change settings.