Cerbuino and SD card

'm new to the systems. NET and especially C #, I come from the small world of Arduino, but this system is too limited for my applications.,
So I turned to Cerbuino. Everything works correctly except little close … SDCARD … Unable to read or write, I can open and close files on the SD card but I’m trying to read a line of text I still get an exception Io.IoException



 private void lecture_data()
        {
            GHI.OSHW.Hardware.StorageDev.MountSD();
            if (VolumeInfo.GetVolumes()[0].IsFormatted)
            {
                _root = VolumeInfo.GetVolumes()[0].RootDirectory;
               
                string fic = Path.Combine(_root, "config.txt");
                if (File.Exists(fic))
                {
                    try
                    {
                        using (StreamReader reader = new StreamReader(fic))
                        {
                            Eco_on = Int32.Parse(reader.ReadLine());
                            Eco_off = Int32.Parse(reader.ReadLine());
                            for (int i = 0; i < 6; i++)
                                capteur[i] = Int32.Parse(reader.ReadLine());

                        }
                    }
                    catch (Exception e)
                    {
                        Debug.Print("Exception :");
                        Debug.Print(e.Message);
                    }
                }
            }

        }

What’s wrong on my code?? i’m lost …

PS : Sorry for my English I’m French … ceci explique cela…

Hi Jean - Welcome :slight_smile:

I use something like this to read a file and parse each line to an array of strings.

 private void ParseFile(int filename)
        {
            string fileName = Path.Combine(_root, filename + ".txt");
            FileStream fileStream = new FileStream(fileName, FileMode.Open);
            byte[] data = new byte[fileStream.Length];
            fileStream.Read(data, 0, data.Length);
            fileStream.Close();
            _fileContents = new string(Encoding.UTF8.GetChars(data));
            _lines = _fileContents.Split('\n');
            for (int i = 0; i < _lines.Length; i ++)
            {
                _lines[i] = _lines[i].Trim(); 
            }
        }

PS - it’s a bit rough and should be have using statements etc.

And here for writing http://www.tinyclr.com/forum/topic?id=8556&page=2#msg84904

Thanx Justin … I’ll try your code … and sorry don’t seen the topic for writing…

Here it is…

This writes a Guid to a file on the Bee every 5 seconds...and I even tested it to make sure it works 

using System;
using System.IO;
using Microsoft.SPOT.IO;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
 
namespace SD_Test
{
    public partial class Program
    {
        private GT.Timer _timer;
        private string _root;
 
        void ProgramStarted()
        {
            GHI.OSHW.Hardware.StorageDev.MountSD();
            if (VolumeInfo.GetVolumes()[0].IsFormatted)
            {
                _root = VolumeInfo.GetVolumes()[0].RootDirectory;
                _timer = new GT.Timer(5000);
                _timer.Tick += new GT.Timer.TickEventHandler(TimerTick);
                _timer.Start();
            }
        }
 
        void TimerTick(GT.Timer timer)
        {
            string fileName = Path.Combine(_root, "file.txt");
            Stream stream;
            if(File.Exists(fileName))
            {
                stream = File.OpenWrite(fileName);
                stream.Position = stream.Length;
            }
            else
            {
                stream = File.Create(fileName);
            }
            using(var writer = new StreamWriter(stream))
            {
                writer.WriteLine(Guid.NewGuid().ToString());
            }
            stream.Dispose();
        }
    }
}

Your question is missing one or more of these details:[ul]
The name of the product.
The code you’re trying to run.
Details on your setup.[/ul]
(Generated by QuickReply)

Ooops … sorry for the reply bellow I don’t find the button “Erase wrong post” ^^

Seriously thanx a lot Justin your codes works perfectly but I don’t anderstand what’s wrong in mine

Good news :slight_smile:
Form a quick look i would say your issue is your not opening the file.