Hi,
I'm looking for a configuration manager that will allow me to store settings and data using the SD Card.
Something similar to the .NET Properties class.
Is there anything ready?
T.
  | |||||||||||||||||||||||||||
Configuration Management
Started by Tal Tikotzki, Jan 26 2013 01:45 PM
config xml settings Properties
4 replies to this topic
#1Posted 26 January 2013 - 01:45 PM Hi, I'm looking for a configuration manager that will allow me to store settings and data using the SD Card. Something similar to the .NET Properties class.
Is there anything ready?
T. #2Posted 26 January 2013 - 02:53 PM i dont understand configuration manager, but you can store settings on sd card, and read them also..
when you keep the settings format easy - its also easy to read #3Posted 26 January 2013 - 09:30 PM NooM, The configuration manager should be a simple class that allows you to store at least key-value pairs in a consistent way across the application.
I wrote something myself which I believe would be useful:
using System;using System.Collections;using System.IO;namespace Utils{ public class ConfigManager : IDisposable { private bool _disposed; private StringDictionary _dataset; private string _configFilename; private readonly char _splitter = '='; public ConfigManager(string configFilename) { _dataset = new StringDictionary(); _configFilename = configFilename; Reload(); } public void Save() { using (var file = new StreamWriter(_configFilename, false)) { foreach (DictionaryEntry dataElement in _dataset) { var line = (string)dataElement.Key + _splitter + (string)dataElement.Value; file.WriteLine(line); } file.Close(); } } public void Reload() { if (!File.Exists(_configFilename)) return; _dataset.Clear(); using (var file = new StreamReader(_configFilename)) { if (file.BaseStream.Length > 0) { do { var str = file.ReadLine(); var idx = str.IndexOf(_splitter); if (idx >= 0) { var key = prepareKey(str.Substring(0, idx)); var value = str.Substring(idx + 1, str.Length - (idx + 1)); _dataset.Add(key, value); } } while (!file.EndOfStream); } file.Close(); } } public void SetValue (string key, string value) { key = prepareKey(key); if (_dataset.ContainsKey(key)) { _dataset[key] = value.Trim(); } else { _dataset.Add(key, value.Trim()); } } public string GetValue (string key) { return _dataset[prepareKey(key)]; } private string prepareKey (string key) { return key.Trim().ToLower(); } #region Implementation of IDisposable /// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> /// <filterpriority>2</filterpriority> public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose (bool disposing) { if (!_disposed) { if (disposing) { _dataset.Clear(); } } _disposed = true; } ~ConfigManager() { Dispose(false); } #endregion }}
An example of how to use it: using (var cm = new ConfigManager(@"SDconfconfig.txt")) { cm.SetValue("key", "value"); // some setting example cm.SetValue("Address", "http://127.0.0.1"); // some setting example cm.Save(); } using (var cm = new ConfigManager(@"SDconfconfig.txt")) { Debug.Print("Address: " + cm.GetValue("Address")); }
#5Posted 27 January 2013 - 01:14 AM I use XML files for this on the PC, because I can design my own class with all kinds of goodies - arrays, dictionaries, sub-classes, etc - and load/'save them with about 3 lines of code. What makes it so cool is that I can load a class out of a XML stream and then just hand it to a class in my application to configure it.
The Micro Framework is necessarily less full-featured, but it looks like they do provide a similar concept, for serialization of objects into byte arrays: Reflection.Deserialize()&Reflection.Serialize() I haven't used them yet, but it might be worth checking in to it... Also tagged with one or more of these keywords: config, xml, settings, Properties
0 user(s) are reading this topic0 members, 0 guests, 0 anonymous users | |||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||
This webpage is licensed under a Creative Commons Attribution-ShareAlike License. | |||||||||||||||||||||||||||