Best way to save small amount of data

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.
vpekulasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
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?

Bob
0
vpekulasAuthor Commented:
Let's say the XML, can you show me a sample write and read ? Thanks.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
            reader.Close()
            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))
        writer.Close()
    End Sub

End Class
0
Bob LearnedCommented:
Here is another way:

Imports System.Xml

Public Class Settings

#Region "Properties"

  Private m_userName As String = ""
  Public Property UserName() As String
    Get
      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
    Get
      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
      m_document.Load(fileName)

      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)

    m_document.Save(fileName)

  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

    owner.AppendChild(element)

    Return element

  End Function  'AddElement

#End Region

End Class
0
Bob LearnedCommented:
Examples:

  Dim settings As New Settings
  settings.UserName = "Bob"
  settings.FontName = "Tahoma"
  settings.Save("c:\temp\test.xml")

  Dim test As New Settings
  test.Load("c:\temp\test.xml")
  MessageBox.Show("User:  " & test.UserName & ", Font:  " & test.FontName)

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.