Wednesday, August 15, 2012

Windows CE for Mobile - Retrieve values from configuration file

As you already know, Windows CE doesn't support Configuration Manager library of .NET Framework.
And, if you need store some configuration values regarding your application, you need find way to retrieve this values by yourself.

I find some simple and quick way to do so:

I am using my Serialization class (see previous post) to deserialize  my configuration model from XML (config file):


public static PrintingConfigurationModel GetTJPrinterCEConfiguration()
{
     //Looking for path of config file
     var mSettingsPath =  Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()
                                                                                                                                       .GetName()
                                                                                                                                       .CodeBase);
     mSettingsPath += @"\Settings.xml";

     if (!File.Exists(mSettingsPath))
     {
           throw new FileNotFoundException(mSettingsPath + " could not be found.");
     }
        
    var printingConfigurationModel = SerializerUtils.Deserialize<PrintingConfigurationModel>(mSettingsPath)   
                                                                                                                    as PrintingConfigurationModel;

    return printingConfigurationModel;

}

My Configuration Model:


[Serializable()]
    public class PrintingConfigurationModel
    {
        [System.Xml.Serialization.XmlElement("PrinterIp")]
        public string PrinterIp { get; set; }

        [System.Xml.Serialization.XmlElement("PrinterPort")]
        public int PrinterPort { get; set; }

        [System.Xml.Serialization.XmlElement("PrintingImagePath")]
        public string PrintingImagePath { get; set; }

        [System.Xml.Serialization.XmlElement("JpgPrintingImagePath")]
        public string JpgPrintingImagePath { get; set; }
    }


And for the End :-), configuration file (xml):


<?xml version="1.0" encoding="utf-8" ?>
<PrintingConfigurationModel xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <PrinterIp>10.190.16.25</PrinterIp>
  <PrinterPort>9100</PrinterPort>
  <PrintingImagePath>\Program Files\Retalix\HandHeldOffice\PrintData\SavedReport.bmp</PrintingImagePath>
  <JpgPrintingImagePath>
           \Program Files\Retalix\HandHeldOffice\PrintData\JpgSavedReport.jpg
  </JpgPrintingImagePath>
</PrintingConfigurationModel>


Hope this simple thing helps !