AppSetting Replacement for the Compact Framework

Jun 26, 2005

2 min read

.NET1.1

.NET2.0

VS.NET2003

VS2005

VB

Windows

.NETCF

.NET

Visual-Studio

Mobile

Dev

Intermediate

Author picture

by wduros1

Contributor

37k Views

4/ 5

Introduction

I was browsing .NET Compact Framework articles, and found an article on how to mimic the functionality of the appSettings() method, which is missing in the Compact Framework. At the bottom of the article were two requests for the code to be presented in VB.NET, rather than the author's native C#.

I just finished a Smart Device project where I had to save the IP address of a server, so that I could make a SOAP call. I had a cConfig class that I have used in ASP.NET to read and write settings, I reused this class on the Compact Framework without any modifications.

One of the nice things about this class is that you can specify the name of the CONFIG file when the object is created. In my ASP.NET project, this allowed me to have different CONFIG files for different customers.

The first thing to do is to create an XML file to store your settings. This file is of the same format as the System.ConfigurationSettings.AppSettings file:

XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

   <appSettings>
    <!- User application and configured property settings go here.-->
    <!- Example: <add key="settingName" value="settingValue"/> -->
    
    <!-- Configuration and Setup Section -->
    <add key="ServerIPAddr" value="192.168.1.14" />

    </appSettings>
</configuration>

Next, create a new class file and paste the following code into the file:

VBNET
Imports System.Xml
Imports System.Xml.XPath


Public Class cConfig

    Private _StrFile As String
    Private _Doc As XmlDocument = New XmlDocument

    Public Sub New(ByVal StrFile As String)
        _StrFile = StrFile
        _Doc.Load(_StrFile)
    End Sub


    Public Function GetSettingDefault(ByVal StrKey As String, _
                          ByVal StrDefault As String) As String
        Dim Node As XmlNode = _
           Doc.SelectSingleNode(_
           "configuration/appSettings/add[@key='" + StrKey + "']")
        If (Node Is Nothing) Then
            Return StrDefault
        End If
        Return ReadWithDefault(Node.Attributes("value").Value, _
                                                      StrDefault)
    End Function

    
    Public Sub SetSetting(ByVal StrKey As String, _
                             ByVal StrValue As String)
        Dim Node As XmlNode = _
           _Doc.SelectSingleNode(_
           "configuration/appSettings/add[@key='" + StrKey + "']")
        Node.Attributes("value").Value = StrValue
        _Doc.Save(_StrFile)
    End Sub
        

    Private Function ReadWithDefault(ByVal StrValue As String, _
                           ByVal StrDefault As String) As String
        Return IIf(StrValue Is Nothing, StrDefault, StrValue)
    End Function
End Class

To use the class, define a cConfig object and call the GetSettingDefault() method to read a setting. Call SetSetting() method to serialize the setting value. Below I have included the code from a simple options form:

VBNET
Public Class frmOptions
    Private m_sConfigFilePath As String

    Private Sub frmOptions_Load(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles MyBase.Load
     
       m_sConfigFilePath = cApp.GetAppPath() & "config.xml"
       Dim oConfig As New cConfig(m_sConfigFilePath)
       Me.txtServerIPAddress.Text = _
           oConfig.GetSettingDefault ("ServerIPAddr", "")
    
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnOK.Click
    
        Dim oConfig As New cConfig(m_sConfigFilePath)
        oConfig.SetSetting("ServerIPAddr", _
                    Me.txtServerIPAddress.Text)
        Me.Close()
    
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnCancel.Click
    
        Me.Close()
    End Sub
End Class

You may notice that in the form load event, I am calling GetAppPath(), and the code for that is included below:

VBNET
Public Class cApp
  ' Return full path to application directory. 
  Public Shared Function GetAppPath() As String
    Dim sTemp As String = _
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
    sTemp = sTemp.Substring(0, sTemp.LastIndexOf("\") + 1)
    Return sTemp
  End Function
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)