Netduino home hardware projects downloads community

Jump to content

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

Bi Qin

Member Since 17 May 2013
Offline Last Active Sep 09 2013 12:37 PM

Topics I've Started

MicroSD Card File Lost/Corrupted?

20 May 2013 - 08:26 AM

Hello everyone.


I tried to create&write&read files with micro sd card on NP2+,using a 2gb sandisk micro sd card with FAT format.


From the debug output information,I can see that file did created and exist.But when I remove the card from NP2+ and try to read the content from my PC,I found nothing,sometimes file exist but corrupted.

Even not removing the card, just re-deploy the same code again(as the chip will reboot),the file created last time  disappered(I detect whether file exist before I create it).


After adding a 1000ms sleep,the situation improves just a little bit,which means if I write it for several times, I may get one time success saved.


So,is there anything wrong with my code or any tricks for NP2+ micso sd file write?


using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using SecretLabs.NETMF.IO; namespace SDFileLearn {   public class Program   {   public static void Main()   {   DirectoryInfo rootDirectory = new DirectoryInfo(@"SD");   if(!Directory.Exists(@"SDNewDir"))   { DirectoryInfo xDir= Directory.CreateDirectory(@"SDNewDir");   }   if (File.Exists(@"SDTestNew.txt"))   {   Debug.Print("File Exist!");   }   else   {   Debug.Print("Not Exist,Create New!");   }   StreamWriter xWrite = new StreamWriter(System.IO.File.Open(@"SDTestNew.txt", FileMode.Append));   xWrite.Write("Hello New Worldrn");   xWrite.Flush();   xWrite.Close();   Thread.Sleep(1000);   if (File.Exists(@"SDTestNew.txt"))   {   StreamReader xRead = new StreamReader(@"SDTestNew.txt");   Debug.Print(xRead.ReadLine());   xRead.Close();   Thread.Sleep(1000);   }   RecurseFolders(rootDirectory);   if (File.Exists(@"SDTestNew.txt"))   {   Debug.Print("File Exist!");   }   else   {   Debug.Print("Not Exist!");   }   Thread.Sleep(1000);   }   private static void RecurseFolders(DirectoryInfo directory)   {   if (directory.Exists)   {   Debug.Print(directory.FullName);   foreach (FileInfo file in directory.GetFiles())   {   Debug.Print(file.FullName);   }   foreach (DirectoryInfo subDirectory in directory.GetDirectories())   {   RecurseFolders(subDirectory);   }   }   }   } }  


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.