Click here to Skip to main content
6,292,811 members and growing! (11,522 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate

AppSetting Replacement for the Compact Framework

By wduros1

A class to replace the appSettings() method in Compact Framework.
VB, Windows, .NET CF, Mobile, .NET 1.1, .NET 2.0VS.NET2003, VS2005, Dev
Posted:26 Jun 2005
Views:21,265
Bookmarked:15 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 2.41 Rating: 4.00 out of 5
1 vote, 25.0%
1

2

3

4
3 votes, 75.0%
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 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

wduros1


Member
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.
Occupation: Web Developer
Location: United States United States

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • Pocket 1945 - A C# .NET CF Shooter
    An article on Pocket PC game development
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralCan someone please provide some code PinmemberHamish Ahern5:16 29 Jan '09  
GeneralCE supports XMLDocument.SelectSingleNode?? PinmemberDinesha Ranathunga20:10 22 Feb '06  
Generalsmart device framework Pinmemberalcoh11:31 29 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jun 2005
Editor: Rinish Biju
Copyright 2005 by wduros1
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project