Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Configuration Management

config xml settings Properties

  • Please log in to reply
4 replies to this topic

#1 Tal Tikotzki

Tal Tikotzki

    Member

  • Members
  • PipPip
  • 10 posts

Posted 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.



#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 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 :)



#3 Tal Tikotzki

Tal Tikotzki

    Member

  • Members
  • PipPip
  • 10 posts

Posted 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"));            }


#4 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 26 January 2013 - 11:23 PM

i like it :)



#5 HiredMind

HiredMind

    Member

  • Members
  • PipPip
  • 25 posts

Posted 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...







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.