INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How-to

A very simple XML based INI setting functionality by ThatRickGuy
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 FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close