content top
What is my remote IP? Creating Java web-service for JBoss Deploy Java webservice on JBoss SSH/SFTP client in VB.net with sources

Store your application config as XML

As I stated in the previous post, the new project of mine I am rewamping most of my old libraries and the configuration is no different. Usually I stored configuration using old INI files, but well – thats a bit outdated now in 2009. I still wanted a similar way to store and get my configuration; so basically I wanted the INI way of storing configuration though in a XML type of format. After browsing the internet I found some different ways people had solved this, though I also wanted the ability to supply a default value in case the value has not been registered. Also, if the setting had not been set already in the file, it should also be automatically set using the default value. This to make it easier and quicker to build a settings.xml file that can quickly be included with applications.

ProgramSettings.vb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'// Copyright (c) 2009 - Kjell Arne Brudvik
'// Email: kjell.arne@brudvik.org - WWW: http://www.brudvik.org/
'// This is released open-source, free to use for anyone whom wants.
 
Imports System.IO
 
Public Class ProgramSettings
 
    Private filename As String = ""
 
    Public Sub New(ByVal filename As String)
        Me.filename = filename
    End Sub
 
    Public Function getSetting(ByVal key As String, ByVal defaultValue As String) As String
        Try
            Dim data As String = ""
            Dim settings As New DataSet
            If File.Exists(filename) Then
                settings.ReadXml(filename)
            Else
                settings.Tables.Add("settings")
                settings.Tables(0).Columns.Add("key", GetType(String))
                settings.Tables(0).Columns.Add("value", GetType(String))
            End If
            Dim row() As DataRow = settings.Tables("settings").Select("key = '" & key & "'")
            If row.Length = 1 Then data = row(0)("value").ToString
            If data.Trim.Equals("") Then
                data = defaultValue
                Call setSetting(key, defaultValue)
            End If
            Return data
        Catch ex As Exception
            Call setSetting(key, defaultValue)
            Return defaultValue
        End Try
    End Function
 
    Public Sub setSetting(ByVal key As String, ByVal value As String)
        Try
            Dim settings As New DataSet
            If File.Exists(filename) Then
                settings.ReadXml(filename)
            Else
                settings.Tables.Add("settings")
                settings.Tables(0).Columns.Add("key", GetType(String))
                settings.Tables(0).Columns.Add("value", GetType(String))
            End If
            Dim row() As DataRow = settings.Tables(0).Select("key = '" & key & "'")
            If row.Length = 1 Then
                row(0)("value") = value
            Else
                Dim setting As DataRow = settings.Tables("settings").NewRow
                setting("key") = key
                setting("value") = value
                settings.Tables("settings").Rows.Add(setting)
            End If
            settings.WriteXml(filename)
        Catch ex As Exception
 
        End Try
    End Sub
 
End Class

How do I use it?
It’s rather simple to use this Class, and simplicity is what I strive for in the code that I release. As you see below you simply call the constructor with the path for a file, and then using the methods getSetting and setSetting you can easily get and set settings in an XML environment. Thus this class can easily be used to finally get rid of your INI configuration routines.

1
2
3
Dim settings as ProgramSettings = New ProgramSettings("settings.xml")
Dim value as String = settings.getSetting("author", "Kjell Arne Brudvik")
settings.setSetting("author", "Some other author")
Skrevet av Kjell Arne Brudvik den 23.03.2009 liker denne artikkelen.
content top