Click here to Skip to main content
15,886,362 members
Articles / Mobile Apps

AppSetting Replacement for the Compact Framework

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
25 Jun 2005CPOL1 min read 36.9K   19   3
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
<?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:

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

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

VB
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)


Written By
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.

Comments and Discussions

 
QuestionCan someone please provide some code Pin
Hamish Ahern29-Jan-09 4:16
Hamish Ahern29-Jan-09 4:16 
QuestionCE supports XMLDocument.SelectSingleNode?? Pin
Dinesha Ranathunga22-Feb-06 19:10
Dinesha Ranathunga22-Feb-06 19:10 
Generalsmart device framework Pin
Ajek29-Jun-05 10:31
Ajek29-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.