Click here to Skip to main content
15,892,537 members
Articles / Programming Languages / Visual Basic

Serialization for Program Settings

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
21 May 2012CPOL 8.5K   95   2  
Serialization for program settings.
<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        ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free other state (managed objects).
            End If

            ' TODO: free your own state (unmanaged objects).
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region
#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions