Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A very simple XML based INI setting functionality

How-to

A very simple XML based INI setting functionality

by  ThatRickGuy  Posted    (Edited  )
This code is designed to create/update/read from an XML file that can be used to store key-value pairs in the same way that you would traditionally use an INI file. The primary difference is that this uses Microsoft's Dataset object and the WriteXML and ReadXML methods. New and improved for .Net 2.0, this also uses the applications data folder under the user's profile. This code should work fine even on networks where the user has limited rights to the path that the application is installed in. These settings will also carry from machine to machine if the user has a roaming profile.

Code:
Public Class Settings
  Public Shared Function GetSetting(ByVal Key As String) As String
    Dim sReturn As String = String.Empty
    Dim dsSettings As New DataSet
    If System.IO.File.Exists(Application.LocalUserAppDataPath & "\Settings.xml") Then
      dsSettings.ReadXml(Application.LocalUserAppDataPath & "\Settings.xml")
    Else
      dsSettings.Tables.Add("Settings")
      dsSettings.Tables(0).Columns.Add("Key", GetType(String))
      dsSettings.Tables(0).Columns.Add("Value", GetType(String))
    End If

    Dim dr() As DataRow = dsSettings.Tables("Settings").Select("Key = '" & Key & "'")
    If dr.Length = 1 Then sReturn = dr(0)("Value").ToString

    Return sReturn
  End Function

  Public Shared Sub SetSetting(ByVal Key As String, ByVal Value As String)
    Dim dsSettings As New DataSet
    If System.IO.File.Exists(Application.LocalUserAppDataPath & "\Settings.xml") Then
      dsSettings.ReadXml(Application.LocalUserAppDataPath & "\Settings.xml")
    Else
      dsSettings.Tables.Add("Settings")
      dsSettings.Tables(0).Columns.Add("Key", GetType(String))
      dsSettings.Tables(0).Columns.Add("Value", GetType(String))
    End If

    Dim dr() As DataRow = dsSettings.Tables(0).Select("Key = '" & Key & "'")
    If dr.Length = 1 Then
      dr(0)("Value") = Value
    Else
      Dim drSetting As DataRow = dsSettings.Tables("Settings").NewRow
      drSetting("Key") = Key
      drSetting("Value") = Value
      dsSettings.Tables("Settings").Rows.Add(drSetting)
    End If
    dsSettings.WriteXml(Application.LocalUserAppDataPath & "\Settings.xml")
  End Sub
End Class

Usage is pretty simple with providing the key for the value you want or the key and the value you want to set. For example:

Code:
Settings.SetSetting("MyValue", "1234567890")
msgbox(Settings.GetSetting("MyValue"))
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top