Best thing to simulate an Ini file

  • Thread starter Thread starter Alexander Muylaert
  • Start date Start date
A

Alexander Muylaert

Hi

What would be the best way to have something like an ini file that works
both on the full and on the compact1 framework?

I need to save 1 - x different configuration files.

kind regards en many thanks

Alexander
 
But the XML - serializer is not available on the compact framework?

Kind regards

Alexander
 
Use a simple class to read and write to a configuration file on either the
device or the desktop. Here's a sample of the xml file and a class to
read/write
named/value pairs.

-Darren Shaffer

Note that if you have many fields to store, you may want to refactor this
code to
use XmlTextReader and XmlTextWriter which are more efficient than
XmlDocument on larger sets. The code below assumes your xml file is named
"config.xml".

///////////////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<configuation>
<appSettings key="MapPointDataSource" value="North America"/>
<appSettings key="MapPointCountry" value="United States"/>
<appSettings key="MapPointLanguage" value="en"/>
<appSettings key="MapStyle" value="Road"/>
<appSettings key="MapFont" value="Smaller"/>
<appSettings key="UnitOfMeasure" value="Metric - Kilometers"/>
<appSettings key="GPSPrecision" value="5"/>
<appSettings key="GPSSampleFreq" value="5"/>
<appSettings key="RefreshMap" value="true"/>
<appSettings key="LastLat" value="39.76547"/>
<appSettings key="LastLong" value="-104.97158"/>
<appSettings key="URL" value="http://staging.mappoint.net"/>
</configuation>
///////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

namespace Utilities
{
class Configuration
{
private static string _appPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
private static string _myConfigFile = _appPath + "\\config.xml";

public Configuration()
{

}
public static string GetAppSetting(string strKey)
{
string sValue = "";
XmlDocument configDocument = new XmlDocument();
configDocument.Load(_myConfigFile);

foreach (XmlNode node in configDocument.ChildNodes)
{
foreach (XmlNode childnode in node.ChildNodes)
{
if
(childnode.Attributes["key"].Value.ToLower().Equals(strKey.ToLower()))
{
sValue = childnode.Attributes["value"].Value;
break;
}
}
if (sValue.Length > 0)
{
break;
}
}
return sValue;
}

public static bool SetAppSetting(string sKey, string sValue)
{
if ( sKey == null || sValue == null )
return false;

XmlDocument configDocument = new XmlDocument();
configDocument.Load(_myConfigFile);

foreach (XmlNode node in configDocument.ChildNodes)
{
foreach (XmlNode childnode in node.ChildNodes)
{
if (childnode.Attributes["key"].Value.ToLower().Equals(sKey.ToLower()))
{
childnode.Attributes["value"].Value = sValue;
configDocument.Save(_myConfigFile);
return true;
}
}
}
return false;
}

}
}
 
Thanks


Darren Shaffer said:
Use a simple class to read and write to a configuration file on either the
device or the desktop. Here's a sample of the xml file and a class to
read/write
named/value pairs.

-Darren Shaffer

Note that if you have many fields to store, you may want to refactor this
code to
use XmlTextReader and XmlTextWriter which are more efficient than
XmlDocument on larger sets. The code below assumes your xml file is named
"config.xml".

///////////////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<configuation>
<appSettings key="MapPointDataSource" value="North America"/>
<appSettings key="MapPointCountry" value="United States"/>
<appSettings key="MapPointLanguage" value="en"/>
<appSettings key="MapStyle" value="Road"/>
<appSettings key="MapFont" value="Smaller"/>
<appSettings key="UnitOfMeasure" value="Metric - Kilometers"/>
<appSettings key="GPSPrecision" value="5"/>
<appSettings key="GPSSampleFreq" value="5"/>
<appSettings key="RefreshMap" value="true"/>
<appSettings key="LastLat" value="39.76547"/>
<appSettings key="LastLong" value="-104.97158"/>
<appSettings key="URL" value="http://staging.mappoint.net"/>
</configuation>
///////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

namespace Utilities
{
class Configuration
{
private static string _appPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
private static string _myConfigFile = _appPath + "\\config.xml";

public Configuration()
{

}
public static string GetAppSetting(string strKey)
{
string sValue = "";
XmlDocument configDocument = new XmlDocument();
configDocument.Load(_myConfigFile);

foreach (XmlNode node in configDocument.ChildNodes)
{
foreach (XmlNode childnode in node.ChildNodes)
{
if
(childnode.Attributes["key"].Value.ToLower().Equals(strKey.ToLower()))
{
sValue = childnode.Attributes["value"].Value;
break;
}
}
if (sValue.Length > 0)
{
break;
}
}
return sValue;
}

public static bool SetAppSetting(string sKey, string sValue)
{
if ( sKey == null || sValue == null )
return false;

XmlDocument configDocument = new XmlDocument();
configDocument.Load(_myConfigFile);

foreach (XmlNode node in configDocument.ChildNodes)
{
foreach (XmlNode childnode in node.ChildNodes)
{
if
(childnode.Attributes["key"].Value.ToLower().Equals(sKey.ToLower()))
{
childnode.Attributes["value"].Value = sValue;
configDocument.Save(_myConfigFile);
return true;
}
}
}
return false;
}

}
}



Alexander Muylaert said:
Hi

What would be the best way to have something like an ini file that works
both on the full and on the compact1 framework?

I need to save 1 - x different configuration files.

kind regards en many thanks

Alexander
 
Back
Top