Request: read config file from sd card (Windows file ini like with sections)

I found in the community a config class example allowing reading from sd card,
I’s just like reading a typical win ini file, except it does not allow grouping
keys/values in sections. I found also an old example from the archives of secret labs,
but it does not work (error in finding a file stream class).
Is there some example about it, cohomprensive of “section grouping” and example to compile an ini library and how to call?

I use Netduino2 plus.

Do you have a link to the original post?

Regards,
Mark

Sorry, bad memory both the examples are from old secret labs blog.

The “without sections” one is here.
http://forums.netduino.com/index.php?/topic/5131-using-microsd-to-manage-configurations/

The second example is here: http://forums.netduino.com/index.php?/topic/3680-reading-wriring-ini-file-from-sd-micro/

http://forums.netduino.com/index.php?/topic/5131-using-microsd-to-manage-configurations/

This code compiles (not tested running it) when opened with VS2013.

The Filestream class should be in the System.IO namespace. I’d double check the using statement and also the the references for the project.

Regards,
Mark

http://forums.netduino.com/index.php?/topic/3680-reading-wriring-ini-file-from-sd-micro/

This one compiles after adding

using System.IO;
using System.Collections;

and also adding System.IO to the references for the project.

Regards,
Mark

I have some difficulties to come back from Arduino programming (in wich i used strictly the “weird” combination of old c++ (old because not using STL with all its potential)
and old C.

The features offered by .net are widely more “modern” as they treat strings as class for example and not merely chars arrays).

But i don’t remember something, so consider me a quiet newbie in .net (not really, i have only to remember something).

Syntetically:actually, in the skeleton of a “netduino universal” project
the library listed in your advice is just in using(include). I referenced only one library not present.

But, cause i have difficulties to compile separately the library from calling code
i mixed in the same namespace the library class and the calling class and code (main).

Neverthless, beeing the include directives already there (using)
the vs debugger don’t recognize the filestrem class and suggest to
include it in several ways, all tried but not resolved.

I figure out that several classes can coexist in the same namespace (or not?).

I dont’t understand, maybe i’d try with same peculiar framework, but i now that filestream class is in System.io since the oldest framework. Normally it must exist also in all more recent frameworks.

I pass by some step but i don’t know wich.

I have to include in the nested “calling” class the include directive to the class in the same namespace?

Really confused, as you can see.

I let you see my “mixed classes” code, so you can have clearer ideas.

Thanks a lot for yout patience.

sing System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using SecretLabs.NETMF.IO;
using System.IO;
using System.Collections;

namespace NetduinoApplication9
{
public class Program
{
public static void Main()
{

    }

}
public class CIni
{
    public CIni()
    {
        _Sections = new Hashtable();
    }

    /// <summary>
    /// Loads the Reads the data in the ini file into the IniFile object
    /// </summary>
    /// <param name="filename"></param>
    public void Load(string FileName, bool append)
    {
        if (!append)
            DeleteSections();

        string Section = "";
        string[] keyvaluepair;

        FileStream inFileStream = new FileStream(Path.Combine("\\SD", FileName), FileMode.Open);
        {
            using (StreamReader inStreamReader = new StreamReader(inFileStream))
            {
                string[] lines = inStreamReader.ReadToEnd().ToLower().Split(new char[] { '\n', '\r' });

                foreach (string line in lines)
                {
                    if (line == string.Empty)
                        continue;
                    if (';' == line[0])
                        continue;

                    if ('[' == line[0] && ']' == line[line.Length - 1])
                        Section = line.Substring(1, line.Length - 2);

                    else if (-1 != line.IndexOf("="))
                    {
                        keyvaluepair = line.Split(new char[] { '=' });
                        SetValue(Section, keyvaluepair[0], keyvaluepair[1]);
                    }
                }
                inStreamReader.Close();
                inFileStream.Close();
            }
        }

    }

    /// <summary>
    /// Used to save the data back to the file or your choice
    /// </summary>
    /// <param name="FileName"></param>
    public void Save(string FileName)
    {
        using (FileStream outFileStream = new FileStream(Path.Combine("\\SD", FileName), FileMode.Create))
        {
            using (StreamWriter outStreamWriter = new StreamWriter(outFileStream))
            {
                foreach (object Sections in _Sections.Keys)
                {
                    outStreamWriter.WriteLine("[" + Sections.ToString() + "]");
                    Hashtable keyvalpair = (Hashtable)_Sections[Sections];

                    foreach (object key in keyvalpair.Keys)
                        outStreamWriter.WriteLine(key.ToString() + "=" + keyvalpair[key].ToString());
                }

                outStreamWriter.Close();
                outFileStream.Close();
            }
        }

    }

    public string GetValue(string Section, string key, string defkey = "")
    {
        key = key.ToLower();
        Section = Section.ToLower();

        Hashtable keyvalpair = (Hashtable)_Sections[Section];

        if ((null != keyvalpair) && (keyvalpair.Contains(key)))
            defkey = keyvalpair[key].ToString();

        return defkey;
    }
    public float GetValue(string Section, string key, float defkey)
    {
        try { defkey = (float)double.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }
    public double GetValue(string Section, string key, double defkey)
    {
        try { defkey = double.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }
    public UInt64 GetValue(string Section, string key, UInt64 defkey)
    {
        try { defkey = UInt64.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }
    public UInt32 GetValue(string Section, string key, UInt32 defkey)
    {
        try { defkey = UInt32.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }
    public UInt16 GetValue(string Section, string key, UInt16 defkey)
    {
        try { defkey = UInt16.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }
    public byte GetValue(string Section, string key, byte defkey)
    {
        try { defkey = byte.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }
    public Int64 GetValue(string Section, string key, Int64 defkey)
    {
        try { defkey = Int64.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }
    public Int32 GetValue(string Section, string key, Int32 defkey)
    {
        try { defkey = Int32.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }
    public Int16 GetValue(string Section, string key, Int16 defkey)
    {
        try { defkey = Int16.Parse(GetValue(Section, key, defkey.ToString())); }
        catch (Exception) { }
        return defkey;
    }

    public void SetValue(string Section, string key, string value)
    {
        key = key.ToLower();
        Section = Section.ToLower();

        if (!_Sections.Contains(Section))
            _Sections.Add(Section, new Hashtable());

        Hashtable keyvalpair = (Hashtable)_Sections[Section];

        if (keyvalpair.Contains(key))
            keyvalpair[key] = value;
        else
            keyvalpair.Add(key, value);
    }
    public void SetValue(string Section, string key, float value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, double value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, byte value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, Int16 value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, Int32 value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, Int64 value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, char value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, UInt16 value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, UInt32 value)
    {
        SetValue(Section, key, value.ToString());
    }
    public void SetValue(string Section, string key, UInt64 value)
    {
        SetValue(Section, key, value.ToString());
    }

    public void DeleteSection(string Section)
    {
        Section = Section.ToLower();

        if (_Sections.Contains(Section))
            _Sections.Remove(Section);
    }
    public void DeleteSections()
    {
        _Sections.Clear();
    }

    private Hashtable _Sections = null;

}

}

What about the references in Solution Explorer. I have this compiling and mine look like this:

The System.IO reference contains the Filestream object. If this is missing right click on References and select Add Reference… from the menu.

Regards,
Mark

I think the problem may be references and I’ve posted a screen shot which should give you an indication on where to look.

Namespaces can contain more than one class, enum, types etc. For instance, System.Collections used in the code you have posted contains a number of collections (classes in tis case) and not just the Hashtable that the example code uses.

Regards,
Mark

My god, me idiot! Thank you very very much, finally i solved.

Really sorry Neyin, only if you want to reply, a new debug message appears in building
and uploading to netduino: “Cannot attach the debugger engine”.
I search further for more details

I’ve occasionally seen this problem when I have uploaded a program that has a bug in it and the bug generates an exception early on in the program execution. I would try to erase the application that is deployed using MFDeploy.

Regards,
Mark

1 Like

You making a really GREAT work for netduino users and its diffusion (netduino has an incredibily potential users growth, but sometimes, at this moment,it seems pioneeristic work on something yet non complete).
Once i have a complete idea i promise to translate in italian all the “solving posts”, (italian people preferring programming in stable and wide ide like VS to use) in the community, to encourage more italian programmers to partecipate to the community.

For the moment i’m little confused in taking samples from secret labs
samples and libraries and mixing THEM with the widerrness ones.

The problem (mine but non only) that good programmers focus a lot on code, bypassing the great work
about package, nuget, extensions in general and theiR compatibility.

For example i haven’t yet known how to “load” libraries with ,dfu extension.

So, for example, have solved a lot of issues, but trying changes sometimes succesfully now i cannot find anymore my " SecretLabs.NETMF.Hardware.NetduinoPlus" but only the previous " SecretLabs.NETMF.Hardware.Netduino"… Having to work on Netduino plus
2 i got problms beacuse of the absence in my references of
SecretLabs.NETMF.Hardware.NetduinoPlus. Compiling The debug gives no errors
but silently all refers to netduino and not to netduinoplus. and cannot deploy to it.

I changed also frameworks and use contemporarely (at the end was a desperate solution) vs 2013, vs2015 and vd2017 (community version) trying also to use roslyn compller and the Tinyclr (on vs 2017).
Thera are always a little problem that i can’t manage.

Very Very appereciable yor work to reply and to explain, and your patience.

O , incidentally i realized that i was programming in vb (vreating microproj in vb project seems not to have reference to netduinoplus).

Come back to C# project it works good .(with the simpler sample, “blinky”).

Thanks a lot.

There is currently work underway to help resolve a lot of the things that you are seeing. Visual Studio 2017 support is being worked on. I would hope that we will get template projects for all of the boards currently supported as I also have to change the board type every time I create a new project.

All I can say is watch this space.

Regards,
Mark

1 Like

Oh, this is already my Netduino Bible and it’s becoming my general microboard
home, since i’ve decided to import my modest projects from Arduino to Netduino.

I wait with emotion the complete Visual Studio 2017 support to Netduino.

1 Like