Click here to Skip to main content
Click here to Skip to main content

AppSetting Replacement for the Compact Framework

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

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

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

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
        _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:

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:

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)

Share

About the Author

wduros1
Web Developer 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.
Follow on   Twitter

Comments and Discussions

 
QuestionCan someone please provide some code PinmemberHamish Ahern29-Jan-09 5:16 
QuestionCE supports XMLDocument.SelectSingleNode?? PinmemberDinesha Ranathunga22-Feb-06 20:10 
Generalsmart device framework Pinmemberalcoh29-Jun-05 11:31 

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

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

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