Re: Best thing to simulate an Ini file
From: Alexander Muylaert (Remove_amg_at_pandora.be)
Date: 11/14/04
- Previous message: Alexander Muylaert: "Weird compilation error"
- In reply to: Darren Shaffer: "Re: Best thing to simulate an Ini file"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 14 Nov 2004 08:48:50 +0100
Thanks
"Darren Shaffer" <darrenshaffer@discussions.microsoft.com> wrote in message
news:uEjLbnhyEHA.1524@TK2MSFTNGP09.phx.gbl...
> 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" <Remove_amg@pandora.be> wrote in message
> news:%23$k9aPWyEHA.2568@TK2MSFTNGP11.phx.gbl...
>> 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
>>
>
>
- Previous message: Alexander Muylaert: "Weird compilation error"
- In reply to: Darren Shaffer: "Re: Best thing to simulate an Ini file"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|