Best way to save small amount of data

Posted on 2006-03-22
Medium Priority
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
  • 3
LVL 96

Expert Comment

by:Bob Learned
ID: 16264422
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

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

Expert Comment

by:Mike Tomlinson
ID: 16265318
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
ID: 16270066
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

Bob Learned earned 1000 total points
ID: 16270074

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

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


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

750 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