Click here to Skip to main content
13,190,994 members (57,564 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 25 Jun 2005

AppSetting Replacement for the Compact Framework

, 25 Jun 2005
Rate this:
Please Sign up or sign in to vote.
A class to replace the appSettings() method in Compact Framework.


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 version="1.0" encoding="utf-8" ?>

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


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

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
    End Sub

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

    Public Sub SetSetting(ByVal StrKey As String, _
                             ByVal StrValue As String)
        Dim Node As XmlNode = _
           "configuration/appSettings/add[@key='" + StrKey + "']")
        Node.Attributes("value").Value = StrValue
    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:

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", _
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnCancel.Click
    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:

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


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


About the Author

Software Developer (Senior) Texas Woman's University
United States United States
I wrote my first program when I was a child - Basic on the TRS-80 used line numbers back then. I enjoy the problem solving and creative process that writing software invokes.

You may also be interested in...

Comments and Discussions

QuestionCan someone please provide some code Pin
Hamish Ahern29-Jan-09 4:16
memberHamish Ahern29-Jan-09 4:16 
QuestionCE supports XMLDocument.SelectSingleNode?? Pin
Dinesha Ranathunga22-Feb-06 19:10
memberDinesha Ranathunga22-Feb-06 19:10 
Generalsmart device framework Pin
alcoh29-Jun-05 10:31
memberalcoh29-Jun-05 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171016.2 | Last Updated 26 Jun 2005
Article Copyright 2005 by wduros1
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid