INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • Turn Off Ad Banners
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Keep up the good work - excellent site - i'd been looking for something like this for ages !..."

Geography

Where in the world do Tek-Tips members come from?

Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ

How-to

A very simple XML based INI setting functionality
Posted: 14 Jun 06 (Edited 4 Jun 07)

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"))

Back to Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ Index
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 Forum
My FAQ Archive
Email This FAQ To A Friend

My Archive