Best way to save small amount of data

Posted on 2006-03-22
Last Modified: 2010-04-23
I have a small window where I ask the user set a few options, what's the best way and
the most common way to save the data so the application is aware of it the next time it starts.
How to save and load data is my question, really :) Thanks.
Question by:vpekulas
    LVL 96

    Expert Comment

    by:Bob Learned
    The "Best way" involves a judgement call, and everybody has their opinions

    1) Write an XML file
    2) Write a simple text file
    3) Save to registry (not the best in my opinion)

    What kind of options are you talking about?


    Author Comment

    Let's say the XML, can you show me a sample write and read ? Thanks.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Here is a simple example of saving the "start position" of a form via XML serialization:

    Imports System.IO
    Imports System.Xml.Serialization

    Public Class Form1

        Private filename As String = "c:\someFile.xml"

        Public Class FormSettings
            Public FormLocation As Point
            Public FormSize As Size

            Public Sub New()
            End Sub

            Public Sub New(ByVal f As Form)
                Me.FormLocation = f.Location
                Me.FormSize = f.Size
            End Sub
        End Class

        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If File.Exists(filename) Then
                Dim xml As XmlSerializer = New XmlSerializer(GetType(FormSettings))
                Dim reader As TextReader = New StreamReader(filename)
                Dim fs As FormSettings = CType(xml.Deserialize(reader), FormSettings)
                Me.Location = fs.FormLocation
                Me.Size = fs.FormSize
            End If
        End Sub

        Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Dim xml As XmlSerializer = New XmlSerializer(GetType(FormSettings))
            Dim writer As TextWriter = New StreamWriter(filename)
            xml.Serialize(writer, New FormSettings(Me))
        End Sub

    End Class
    LVL 96

    Expert Comment

    by:Bob Learned
    Here is another way:

    Imports System.Xml

    Public Class Settings

    #Region "Properties"

      Private m_userName As String = ""
      Public Property UserName() As String
          Return m_userName
        End Get
        Set(ByVal Value As String)
          m_userName = Value
        End Set
      End Property  'UserName

      Private m_fontName As String = ""
      Public Property FontName() As String
          Return m_fontName
        End Get
        Set(ByVal Value As String)
          m_fontName = Value
        End Set
      End Property  'FontName

    #End Region

    #Region "Methods"

      Private m_document As New XmlDocument

      Public Sub Load(ByVal fileName As String)

        If IO.File.Exists(fileName) Then

          Me.UserName = GetNodeValue("UserName")
          Me.FontName = GetNodeValue("FontName")
        End If

      End Sub  'Load

      Private Function GetNodeValue(ByVal name As String) As String

        Dim node As XmlNode = m_document.SelectSingleNode("//" & name)

        If Not node Is Nothing Then
          Return node.InnerText
        End If

        Return ""

      End Function  'GetNodeValue

      Public Sub Save(ByVal fileName As String)

        m_document = New XmlDocument

        Dim settings As XmlElement = Me.AddElement(m_document, "Settings", "")
        Dim userName As XmlElement = Me.AddElement(settings, "UserName", Me.UserName)
        Dim fontName As XmlElement = Me.AddElement(settings, "FontName", Me.FontName)


      End Sub  'Save

      Private Function AddElement(ByVal owner As XmlNode, ByVal name As String, ByVal value As String) As XmlElement

        Dim element As XmlElement = m_document.CreateElement(name)

        If value.Length > 0 Then
          element.InnerText = value
        End If


        Return element

      End Function  'AddElement

    #End Region

    End Class
    LVL 96

    Accepted Solution


      Dim settings As New Settings
      settings.UserName = "Bob"
      settings.FontName = "Tahoma"

      Dim test As New Settings
      MessageBox.Show("User:  " & test.UserName & ", Font:  " & test.FontName)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now