Simple XML Serialized Settings





6
Date Submitted Sat. Nov. 18th, 2006 5:40 AM
Revision 1 of 1
Scripter Pio
Tags Serialization | VB.NET | XML
Comments 0 comments
I use something similar to this in an application named Shock 3. It provides you with your own Custom settings that can handle your own Custom types as well.

    Imports System.XML.Serialization
 

    Public Class SettingsObject
        Public MainLocation As Location
        Public MainSize As Size
        Public LastUsed As DateTime
    End Class
    Dim MySettings As SettingsObject
 

    Public Shared Function ReadSettings(ByVal fileName As String) As Boolean
        Dim serializer As New XmlSerializer(GetType(SettingsObject))
        Dim reader As New FileStream(fileName, FileMode.Open)

        Try
            MySettings = CType(serializer.Deserialize(reader), SettingsObject)
            Call reader.Close()
            Return True
        Catch ex As Exception
            Call reader.Close()
            Debug.WriteLine(ex.ToString)
            Return False
        End Try
    End Function
 

    Public Shared Function SaveSettings(ByVal fileName As String) As Boolean
        Try
            Dim serializer As New XmlSerializer(GetType(SettingsObject))
            Dim writer As New StreamWriter(fileName, False)

            Call serializer.Serialize(writer, SettingsInstance)
            Call writer.Close()
            Return True
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            Return False
        End Try
    End Function
 

MySettings.MainLocation = frmMain.Location
MySettings.MainSize = frmMain.Size
MySettings.LastUsed = DateTime.Now
'These settings can be changed at any time. It doesn't have to be before the actual saving.
SaveSettings("C:\testing.xml") 'Save our Settings
 

LoadSettings("C:\settings.xml")
Debug.WriteLine(MySettings.LastUsed.ToLongDateString, "Last Used")
 
br/>
Good luck,
-Allen

Allen Gammel

p-soft.org
Allen / Pio
email: intolerance@gmail.com

Comments

There are currently no comments for this snippet.

Voting