?
Solved

Best way to save small amount of data

Posted on 2006-03-22
5
Medium Priority
?
241 Views
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.
0
Comment
Question by:vpekulas
  • 3
5 Comments
 
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?

Bob
0
 

Author Comment

by:vpekulas
ID: 16264440
Let's say the XML, can you show me a sample write and read ? Thanks.
0
 
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)
            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
 
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
    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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 16270074
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

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