A simple implementation of config files for windows mobile

Have you ever written a mobile application using .NET compact framework and tried to use config files? I think there are many scenarios where you want to have a configurable mobile application. So I can’t understand why Microsoft hasn’t implemented (even a subset of) the .NET application configuration handling in CF.
Here is a short implementation which gives you very basic configuration abilities in CF.

First I build a simple class for reading the configuration using the XmlDocument class. This class is designed to be static, so you can access the configuration without having to create an instance of the MobileConfiguration class.

   1: public static class MobileConfiguration
   2: {
   3:     public static NameValueCollection Settings;
   4:  
   5:     static MobileConfiguration()
   6:     {
   7:         string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
   8:         string configFile = Path.Combine(appPath, "App.config");
   9:  
  10:         if (!File.Exists(configFile))
  11:         {
  12:             throw new FileNotFoundException(string.Format("Application configuration file '{0}' not found.", configFile));
  13:         }
  14:  
  15:         XmlDocument xmlDocument = new XmlDocument();
  16:         xmlDocument.Load(configFile);
  17:         XmlNodeList nodeList = xmlDocument.GetElementsByTagName("appSettings");
  18:         Settings = new NameValueCollection();
  19:  
  20:         foreach (XmlNode node in nodeList)
  21:         {
  22:             foreach (XmlNode key in node.ChildNodes)
  23:             {
  24:                 Settings.Add(key.Attributes["key"].Value, key.Attributes["value"].Value);
  25:             }
  26:         }
  27:     }
  28: }

Now you just need to add an app.config to the root of your mobile application. You can do this by adding a new xml-document with the name “app.config” (or whatever name you would like). Don’t forget to set the Build Action of this file to “Contend” so that the file will be deployed to the mobile device.

Here is a sample App.config:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:   <appSettings>
   4:     <add key="ServerName" value="myServer.MyDomain" />
   5:     <add key="ServerPort" value="0815" />
   6:     <add key="UserName" value="MyUser" />
   7:     <add key="Password" value="MyPwd" />
   8:   </appSettings>
   9: </configuration>

The access to these settings in the mobile application is very easy:

   1: ...
   2: string userName = MobileConfiguration.Settings["UserName"];
   3: ...

I know this is no solution for more complex scenarios and it can’t be compared with the power of ConfigSections in the “real” Framework, but with this solution it is possible to use simple configuration for windows mobile based applications.


Posted May 26 2009, 03:06 PM by alehmann
Filed under:

Comments

mayur wrote re: A simple implementation of config files for windows mobile
on 06-18-2009 13:06

really good

Fremy Jose wrote re: A simple implementation of config files for windows mobile
on 10-02-2009 15:55

This is really good, developers need like these sniplets in time to get development real fast. Congrats Alehmann

Phil wrote re: A simple implementation of config files for windows mobile
on 12-02-2009 2:13

How would I convert this to VB which does not support static classes..

alehmann wrote re: A simple implementation of config files for windows mobile
on 12-02-2009 8:29

You can try this VB implementation, but please keep in mind I'm a C# developer so no guarantee for this code ... ;-)

Public Class MobileConfiguration

   ' Methods

   Shared Sub New()

       Dim configFile As String = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly.GetName.CodeBase), "App.config")

       If Not File.Exists(configFile) Then

           Throw New FileNotFoundException(String.Format("Application configuration file '{0}' not found.", configFile))

       End If

       Dim xmlDocument As New XmlDocument

       xmlDocument.Load(configFile)

       Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("appSettings")

       MobileConfiguration.Settings = New NameValueCollection

       Dim node As XmlNode

       For Each node In nodeList

           Dim key As XmlNode

           For Each key In node.ChildNodes

               MobileConfiguration.Settings.Add(key.Attributes.ItemOf("key").Value, key.Attributes.ItemOf("value").Value)

           Next

       Next

   End Sub

   ' Fields

   Public Shared Settings As NameValueCollection

End Class

teo wrote re: A simple implementation of config files for windows mobile
on 09-29-2010 23:08

I'm trying to use an App.config file for an application for Windows Mobile 6 Professional... I've set the "Content" Build Action to the App.config file but the file isn't deployed to the simulator... what should I do???

thanks

teo wrote re: A simple implementation of config files for windows mobile
on 09-29-2010 23:13

found!

I have also to set the Copy to Output Directory setting... ;-)

Sandip Shewale wrote re: A simple implementation of config files for windows mobile
on 10-06-2010 7:20

Really Good Application for Windows Mobile,

Thank You very much!!

B Kundu wrote re: A simple implementation of config files for windows mobile
on 10-08-2010 15:02

This is helpful.

But I want some help for the following :

I want create a project in VC# using VS2008 for WM6.

In my project , I want to create a customized home screen and I want to load some Icons dynamically in my Home Screen . And those icons I want store in a resource file . I want to use Configfile using Xml file.

Can anybody help me on this ?

B Kundu wrote re: A simple implementation of config files for windows mobile
on 10-08-2010 15:03

This is helpful.

But I want some help for the following :

I want create a project in VC# using VS2008 for WM6.

In my project , I want to create a customized home screen and I want to load some Icons dynamically in my Home Screen . And those icons I want store in a resource file . I want to use Configfile using Xml file.

Can anybody help me on this ?

Doug Potter wrote re: A simple implementation of config files for windows mobile
on 11-14-2010 1:24

Very nice.  I've not found something this clear after looking hours and hours of searching.

massimo wrote re: A simple implementation of config files for windows mobile
on 11-25-2010 10:40

It is really great!! Thanks

To be precise this is the VB implementation:

Imports System

Imports System.IO

Imports System.Windows.Forms

Imports System.Reflection

Imports System.Threading

Imports System.Configuration

Imports System.Xml

Public Class MobileConfiguration

   ' Methods

   Public Shared Settings As System.Collections.Specialized.NameValueCollection

   Shared Sub New()

       Dim configFile As String = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly.GetName.CodeBase), "app.config")

       If Not File.Exists(configFile) Then

           Throw New FileNotFoundException(String.Format("Application configuration file '{0}' not found.", configFile))

       End If

       Dim xmlDocument As New XmlDocument

       xmlDocument.Load(configFile)

       Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("appSettings")

       Settings = New System.Collections.Specialized.NameValueCollection

       Dim node As XmlNode

       For Each node In nodeList

           Dim key As XmlNode

           For Each key In node.ChildNodes

               MobileConfiguration.Settings.Add(key.Attributes.ItemOf("key").Value, key.Attributes.ItemOf("value").Value)

           Next

       Next

   End Sub

   ' Fields

End Class

Gregory wrote re: A simple implementation of config files for windows mobile
on 12-13-2010 14:55

Thanks a lot!:)

Andres wrote re: A simple implementation of config files for windows mobile
on 12-22-2010 19:52

Excelent,

thanks a lot, I only do a modification for "ConnectionStrings"

Thanks

Jack wrote re: A simple implementation of config files for windows mobile
on 01-26-2011 17:06

Hi,

When I tried to implement the same code in Window Mobile device 6 Professional, I had problem in deploying the Simulator. The error saying "Application configuration file '\Program Files\Test_App\app.config' not found."

Can someone help me out?

Thanks,

Jack

Fernando wrote re: A simple implementation of config files for windows mobile
on 04-16-2011 12:29

Thank you very much.  I know this comment is a bit late, but I just started working with mobile devices, and was surprised to find no support for app.config on the .net CF.

@Jack:  As someone on the comments mentioned, set the Copy to Output directory property to newer or always and that should solve your problem.

Fernando.

Surendar wrote re: A simple implementation of config files for windows mobile
on 08-03-2011 7:37

Thanks a lot it worked:(

Yanz wrote re: A simple implementation of config files for windows mobile
on 01-05-2012 5:33

how to use in VB.net?

string userName = MobileConfiguration.Settings["UserName"];

Jordi wrote re: A simple implementation of config files for windows mobile
on 02-20-2012 1:16

And the function that saves the settings in vb...

   Shared Sub Save()

       Dim configFile As String = Assembly.GetExecutingAssembly.GetName.CodeBase & ".config"

       If Not File.Exists(configFile) Then

           Throw New FileNotFoundException(String.Format("Application configuration file '{0}' not found.", configFile))

       End If

       Dim xmlDocument As New XmlDocument

       xmlDocument.Load(configFile)

       Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("appSettings")

       For i As Integer = 0 To Settings.Count - 1

           Dim sKey As String = Settings.GetKey(i)

           Dim sValue As String = Settings.Get(i)

           Dim bFound As Boolean = False

           Dim node As XmlNode

           For Each node In nodeList

               Dim key As XmlNode

               For Each key In node.ChildNodes

                   If key.Attributes.ItemOf("key").Value = sKey Then

                       key.Attributes.ItemOf("value").Value = sValue

                       bFound = True

                       Exit For

                   End If

               Next

               If bFound Then Exit For

           Next

           If Not bFound Then

               Dim elem As XmlElement

               Dim attr As XmlAttribute

               'main node

               elem = xmlDocument.CreateElement("add")

               attr = xmlDocument.CreateAttribute("key")

               attr.Value = sKey

               elem.Attributes.Append(attr)

               attr = xmlDocument.CreateAttribute("value")

               attr.Value = sValue

               elem.Attributes.Append(attr)

               xmlDocument.SelectSingleNode("//appSettings").AppendChild(elem)

           End If

       Next

       xmlDocument.Save(configFile)

   End Sub

Martin Milan wrote re: A simple implementation of config files for windows mobile
on 02-29-2012 12:32

Just a quick note to say thanks for this... I was about to implement something similar in my lunch break, but this is pre-written and will do the job I have in mind...

As others have said, the lack of Configuration on CF is a big oversight on MS's part. I've only worked with CF for a few months, and I have been amazed how limited it is in terms of memory, and missing "really basic bits" - like TryParse methods on various types etc. Yes, ok, you can easily reimplement a lot of bits yourself, but you SHOULDN'T HAVE TO...

anil wrote re: A simple implementation of config files for windows mobile
on 10-12-2012 13:04

hi i am very thankful to you for this.

Because i am trying for this for a long time. And now i am able to store my app settings but there is another problem i am facing when i want to update . i Tried many of the codes from the forums it is executing successfully but the file is not updating. So can you please provide me how to update the xml file from my code. please please help me on this

Thanks in advance,

Anilkumar

Rizky Zulkarnaen wrote re: A simple implementation of config files for windows mobile
on 02-20-2013 7:12

I'm facing type load exception error when implement this

Jay wrote re: A simple implementation of config files for windows mobile
on 03-28-2013 16:54

Thank you. This was helpful.  I made a mistake by placing comments in the app.config file.  The parsing logic cound not handle this.  Once I removed my comments the logic worked great.  

bcd wrote re: A simple implementation of config files for windows mobile
on 01-15-2014 4:59

This implementation is pretty usefull and for Jack as Fernando said, you need to set "Copy allways" in the Copy output directory property for that file and also you need to set Build Action as "Content".

Marcelo Casamassa wrote re: A simple implementation of config files for windows mobile
on 01-15-2014 13:35

Congrats this is very useful...

Thank you

Ezgi Keçici wrote re: A simple implementation of config files for windows mobile
on 04-16-2014 16:33

Thank you so much Alehmann. This code is really good and helpful.

Sanya S wrote re: A simple implementation of config files for windows mobile
on 02-25-2015 13:08

Thanks, this is quite simple and good.

developers.de is a .Net Community Blog powered by daenet GmbH.