Ole,
A simple xml file can be used on device for this purpose and also as a place
to store/retrieve
any information you want to persist between invocations of your mobile app.
Below is a configuration
class I add to my projects and a sample XML file. 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".
-Darren Shaffer
////////////////////////////////////////////////////////////////////////////
///////////////////////////////
<?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].FullyQ
ualifiedName);
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;
}
}
}
ORC said:
Does anyone have an example of how to load and save fields in a class to a
file in C# like e.g. this:
public class Config
{
string Var1 = "default"; // default value of Var1
int Var2 = 6; // default value of Var1
}
and could the two functions like e.g. SaveConfigToFile and
LoadConfigFromFile (user defined names) be implemented in the class
itself??
Thanks - I really appreciate the help I got from these groups!
Ole