File.WriteAllBytes "System.IO.IOException"

I have no clue what I am doing wrong here, I am trying to setup a method to create a device ID by writing a GUID to a file (I know it won’t persist firmware flashes), but I always get IOException errors. My top 2 lines don’t change, but I have tried both the Create and the WriteAllBytes lines.

        Guid DeviceID = Guid.NewGuid();
        string DeviceIDFile = "DeviceID.cfg";
        System.IO.File.Create(DeviceIDFile, DeviceID.ToByteArray().Length);

or
System.IO.File.WriteAllBytes(DeviceIDFile, DeviceID.ToByteArray());

Can anyone point me in the right direction for what I am doing wrong?

How do you mount the file system?

Uh, mounting? Ok, new keyword for me to go search.

What board are you using?

CerbuinoBee in Gadgeteer

Did you see this thread:

http://www.tinyclr.com/forum/topic?id=7593&page=3#msg78949

Ok, thanks for that URL, but that leaves me 2 things.

  1. Is there any internal storage that can be accessed? It will be a tiny file (just a guid) so I would prefer to keep it in onboard storage.

  2. When I tried the code in that I get “An unhandled exception of type ‘System.Exception’ occurred in Microsoft.SPOT.Hardware.dll” when it tries to instantiate the FEZCerbuinoBee object.

There was a codeshare post recently that uses Flash for similar purpose:

http://www.tinyclr.com/codeshare/entry/557

Ok that was an old thread.
See this one:

http://www.tinyclr.com/forum/topic?id=7475&page=2

I am mounting the SD card and Microsoft.SPOT.IO.VolumeInfo.GetVolumes()[0].RootDirectory returns “\SD”, but get directories and trying to write both error out. GetVolumes()[0] can get file system, free space, total space, etc.

The internal storage piece looks a bit risky.

Any other ideas?

  • Show you complete code.
  • Provide more information on errors

Error:
An unhandled exception of type ‘System.IO.IOException’ occurred in System.IO.dll

Code:
Debug.Print(“Program Started”);
Guid DeviceID;
string DeviceIDFile = @ “\SD\DeviceID.cfg”;
GHI.OSHW.Hardware.StorageDev.MountSD();
if (System.IO.File.Exists(DeviceIDFile))
DeviceID = new Guid(System.IO.File.ReadAllBytes(DeviceIDFile));
else
{
DeviceID = Guid.NewGuid();
System.IO.File.WriteAllBytes(DeviceIDFile, DeviceID.ToByteArray());
}

Is your sd card formatted?

Yeah, and through VolumeInfo it reports the filesystem as FAT and it returns both total size and total free space as valid numbers so as best as I can tell it seems to be in and valid. It is an 8GB card but the documentation I saw on the SD module indicates that at least the FEZ Spider has no size limit.

Do you have another card that you can try?

I tried another card and I have tried another CerbuinoBee.

I downloaded the 4.1 and 4.2 files today and updated firmware on both today.

Same errors?

It takes a few seconds, after you program starts, for the SD card to be ready for mounting. Usually, you should check to see if the SD card is inserted before mounting.

You can verify that this is a timing issue by adding a Thread.Sleep(5000) before mounting the SD card.

As I said, it is reading the file system type, free and total spaces, root directory, etc, would those be available if it wasn’t ready? Also, I have hit a breakpoint after the insert but before the WriteAllBytes where I do those checks in the immediate window, and I am there for more than 10 seconds. Just for fun I threw in a 30 second loop and then expanded that to a 5 minute loop (just debug.print of the time and then sleep 1000) and it failed after both the 30 second and 5 minute pauses.

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

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();
        }
    }
}

That code is throwing the same error. I’ve got a couple of micro SDs on their way, I’ll see if it works with those.