Main Site Documentation

Error reading from sd card with filestream - Fez domino


#1

Hello,
I´m getting the folowing error when reading from a sd card (the 1st access is ok but all the others fail … coindcidence ?)
Does any boby can help me ?
Thanks

[quote][/quote]

Exception System.IO.IOException - CLR_E_INVALID_DRIVER (7)

#### Message: 
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.FileStream::.ctor [IP: 005c] ####
#### System.IO.FileStream::.ctor [IP: 0009] ####
#### newWeb.webServer+httpServer::convertFileToByteArray [IP: 0007] ####
#### newWeb.webServer+httpServer::ProcessClientGetRequest [IP: 002c] ####
#### newWeb.webServer+httpServer::.ctor [IP: 0072] ####
#### newWeb.webServer::httpHandler [IP: 0004] ####

[quote][/quote]

The code …


  private byte[] convertFileToByteArray(string filePath)
  {
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
      byte[] buf = new byte[fs.Length];
      fs.Read(buf, 0, buf.Length);
      return buf;
    }
  }


#2

I thnk you need to close your filestream. The object gets discarded after the first time thru but the stream isn’t closed; exception results.


#3

You are also constrained by memory. I don’t know how big the file is you are loading into the array but anything above 5k or so is going to cause other problems.


#4

Hello Bret and rRalizer. Thanks for your support.

Bret, as far as I know the using ditective will detroy the filestream as soon as the program leaves the code between the ‘{ }’.Am I right ?

The ‘FilePath’ is a string that points to a file in the SD card; I have a class to read the SD card, and I found out a few minuts ago, if I declare the ‘PersistentStorage sd’ as static the problem does not happen any more. Here is the class code; I´m tring to undestand why !


class microSD
{
static PersistentStorage sd;
private static string _rootDirectory;

public static string rootDirectory
{
  get { return _rootDirectory; }
  set { _rootDirectory = value; }
}

public byte startSD()
{
  try
  {
    sd = new PersistentStorage("SD");
    sd.MountFileSystem();
    if (VolumeInfo.GetVolumes()[0].IsFormatted)
    {
      rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
      return 0;
    }
    else
      return 1;
  }
  catch (Exception e)
  {
    return 2;
  }
}


#5

The use of the static makes the object or primitive “global” for the application and is single in existence. So if you initialize it, the reference is valid throughout the application.

Typed on a iPhone with fat fingers… Excuse typos


#6

Hello Rajesh,
Yes correct, the application is with a good behavior now , thnaks