Originally write by myself and published in codeproject. Reproduced adaptation here to allow the community provide enhancements.
How to Load and Save Settings (or other kind of data) for Compact Framework Devices Development
The code provided is to use XML files as a storage for data (fast). It is useful to use like the .NET Framework appSettings class, but devoted to the compact framework. It is 100% compatible and very lightweight (compared to SQLCE). It solves the problem of storing data permanently in any CFNet device, related to an application -not only settings-, so you can use this class to store other kinds of data in the client Windows Mobile smartdevice.
appSettings
This is easily portable to C# This code -for VB.NET- create an XML file if no one's present. It's very easy to use in less than a minute because you have a default XML settings file that is on a string variable that you can easily customize (inside #MyAppSetting.vb). With that class, you can create/store, load and update data through XML. You can add all the 'data fields' that your application requires and manage in simple 1 or 2 ways:
'Within classPublic Property YourRequiredData() As TypeNeeded Get Return Me.ListItems.Get("YourRequiredData") End Get Set(ByVal value As TypeNeeded) Me.ListItems.Set("YourRequiredData", value) End SetEnd Property
It is very simple to use, and it is well documented (I think). Just add the class MyAppSetting.vb to your project, then:
'At the top of the form, just below form class declarationDim MyAppSettings As New MyAppSettings
To the above instance, you can pass an optionally args; Path string and a FileName string to look for/create (look at the supplied code for more info). If the class did not find any XML file, then automatically create one, just ready to use. What? The default XmlDefaultConfig....
XmlDefaultConfig
In the class there a var that holds the default XML configuration that you can customize (all custom code inside appSettings tab, respecting the format):
var
Private XmlDefaultConfigurationFileString As String = _"<?xml version=""1.0"" encoding=""utf-8"" ?>" & _"<configuration>" & _ "<appSettings>" & _ "<add key=""FileCreated"" value=""" & Now & """ />" & _ "<add key=""FileModified"" value="""" />" & _ "<add key=""UserName"" value="""" />" & _ "<add key=""LoginCount"" value=""0"" />" & _ "</appSettings>" & _"</configuration>"
Now you can load data anywhere:
'Code to Load dataDim YourRequiredData As AnyType = MyAppSettings.YourRequiredData 'if you write that 'property/per/ as indicated above at the top of document Dim YourRequiredDataWithoutTypeAnyProp As AnyType = _ CType(MyAppSettings.ListItems("YourRequiredDataWithoutTypeAnyProp"), AnyType)
You can save by doing:
'Code to Save/Update data to xml settingsMyAppSettings.YourRequiredData = "new data to store .."MyAppSettings.ListItems("YourRequiredDataWithoutTypeAnyProp") = Now.today 'for exampleMyAppSettings.SaveXmlCurrentConfiguration()
3 different methods exposed can be used to save the XML default file (that you could implement for update or save in another method). They are in the following enum:
enum
Enum enumXmlSaveMethod StreamWrite = 1 'Basic XmlTextWriter = 2 'Good XmlDocument = 3 'Good as knowledgeEnd Enum Private XmlSaveMethod As enumXmlSaveMethod = enumXmlSaveMethod.XmlDocument
You can try the three methods just by changing the last assign.
Download source - 35.37 KB
Maheshkumar S Tiwari edited Revision 1. Comment: Added Tag. Image link seems broken, please correct it