developers.de
daenet's .NET Community

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.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
daenet GmbH