[SD Card] How to write/read files
#1
Posted 20 June 2012 - 05:43 PM
#2
Posted 20 June 2012 - 06:46 PM
- Irma Nirmalasari likes this
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#3
Posted 20 June 2012 - 06:57 PM
I can't find SecretLabs.NETMF.IO.dll on my Computer. Where can I download it?
It is not part of the C:\Program Files\Secret Labs\Netduino SDK\Assemblies\v4.1\ folder ...
Sorry for all these beginner questions
LordOfTheFlakes
EDIT: OOPPPS System.IO Ok I added the reference and the errors are gone. Thanks!
#4
Posted 20 June 2012 - 07:14 PM
Eine Ausnahme (erste Chance) des Typs "System.IO.IOException" ist in Microsoft.SPOT.IO.dll aufgetreten.
#### Exception System.IO.IOException - CLR_E_INVALID_DRIVER (1) ####
#### Message:
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.Directory::Exists [IP: 0015] ####
#### System.IO.DirectoryInfo::get_Exists [IP: 0007] ####
#### netduinoIO.Program::RecurseFolders [IP: 0005] ####
#### netduinoIO.Program::Main [IP: 000c] ####
Code:
public static void Main() { DirectoryInfo rootDirectory = new DirectoryInfo(@"\SD\"); RecurseFolders(rootDirectory); Thread.Sleep(Timeout.Infinite); } 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); } } }
Might the memory capacity of my SD card be the problem? (32 GB)
LordOfTheFlakes
#5
Posted 20 June 2012 - 07:25 PM
SDHC, that's only supported with a beta version of the firmware:Might the memory capacity of my SD card be the problem? (32 GB)
http://forums.netdui...re-v411-beta-1/
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#6
Posted 21 June 2012 - 12:42 PM
LordOfTheFlakes
EDIT:
I can use the code above to list the directories. BUT I tried to write an read a file:
1. Writing was sucessfull, I can view the files on my computer.
2. Reading causes this error:
#### Exception System.IO.IOException - CLR_E_INVALID_DRIVER (1) ####
#### Message:
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.File::Exists [IP: 001a] ####
#### netduinoIO.Program::ReadFile [IP: 0009] ####
#### netduinoIO.Program::Main [IP: 0008] ####
I have seen the error in this forum but there was no useful soloution ...
Codes:
private static String rootDir = @"\SD\";
Write:
public static void WriteFile(String sFilename, String sLines) { StreamWriter myFile = new StreamWriter(rootDir + sFilename); myFile.Write(sLines); myFile.Close(); }
Read:
public static string ReadFile(String sFilename) { string sContent = ""; if (File.Exists(sFilename)) { StreamReader myFile = new StreamReader(new FileStream(rootDir + sFilename, FileMode.Open, FileAccess.Read)); sContent = myFile.ReadToEnd(); myFile.Close(); } return sContent; }
#7
Posted 21 June 2012 - 01:02 PM
public static string ReadFile(String sFilename) { string sContent = ""; // Don't use this: //if (File.Exists(sFilename)) // Use this instead: if (File.Exists(rootDir + sFilename)) { StreamReader myFile = new StreamReader(new FileStream(rootDir + sFilename, FileMode.Open, FileAccess.Read)); sContent = myFile.ReadToEnd(); myFile.Close(); } return sContent; }
Did that help?
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#8
Posted 21 June 2012 - 01:10 PM
#9
Posted 21 June 2012 - 02:16 PM
I hate this kind of mistakes. They are soooo needless ...
I dunno, silly mistakes are the ones you learn from
Been using .net since it was known as VB7/ASP Plus and still I forget to add references and wonder why my code fails to build...
For me personally unless the code comes from Jon Skeet or a few other select .net devs i always assume its wrong until proven right, at the very least do a code review and if coming from a VB perspective check the MSDN code examples you often find subtle differences, when i made the jump to C# from VB string escapement always got me i.e.
"C:\Directory\file.txt"
needs to be either
@"C:\Directory\file.txt" //@ prefix means string literal //or "C:\\Directory\\file.txt" //note double \\ to escape the \ which is the C# string control char i.e. \r\n for a line break.
But having made the jump you would have a hard time convincing me to go back
Nak.
#10
Posted 21 June 2012 - 02:33 PM
Should I feel offended?For me personally unless the code comes from Jon Skeet or a few other select .net devs i always assume its wrong until proven right
With Netduino, I find working with the SD always a bit strange, due to the SD mountpoint. A few interesting things I found out:
- All IO in the root folder will fail, except for a GetDirectories and GetFiles call. File.Exists should return false, instead it gives an exception.
- The SD mount point is case sensitive. With my linux knowledge I understand that, but for .NET, I find it odd.
I'm not sure if these things are bugs or not, but it's interesting anyways.
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#11
Posted 21 June 2012 - 03:00 PM
Should I feel offended?
With Netduino, I find working with the SD always a bit strange, due to the SD mountpoint. A few interesting things I found out:
- All IO in the root folder will fail, except for a GetDirectories and GetFiles call. File.Exists should return false, instead it gives an exception.
- The SD mount point is case sensitive. With my linux knowledge I understand that, but for .NET, I find it odd.
I'm not sure if these things are bugs or not, but it's interesting anyways.
Note "few other select .net devs"
I have spent quite lot of time since monday picking apart the SD card firmware and all of that makes a lot of sense, as far as my understanding goes the netmf file system has more in common with *nix than dos based file systems, they have just wrapped it to make it more familiar to .net devs i.e. there is code in there to limit folder path depth to match the desktop experience. SD is the mount point, as you pointed out, so in windows terms trying to call File.Exists("C:") would also yield false or an IO error, yet File.Exists("C:\file.txt") would return true. I Suppose one of the reasons for not hard coding a SD\ prefix to all file path requests is the use case of multiple SD cards/storage devices and would require quite a bit of hacking at core netmf runtimes to provide that functionality and would ultimately break code portability to the wider netmf eco system.
As for case sensitivity I am sure that has more to do with platform optimisation, why do a string comparison or case conversion if you can just type the mount point in correctly? Besides file IO in mono is case sensitive (or it was last time i looked a few years ago)...
Nak.
#12
Posted 12 August 2012 - 05:06 AM
I want to put part of my code on an SD card and have it behave like it was on an EEPROM. Basically, I have this table at the start of my code that defines a whole heap of variables as int's. I use a webserver to change them, but they always need changing, and my Netduino is not always powered so they return to defaults all the time. I'm really new to programming and have been reading a all about these stream writers and such. But I can't work out from the material how I could read and write resources to the SD Card. The ultimate solution to the problem would be to put a .cs file on the card. And then when I changed my ints they might stay the same. But I bet it's going to be harder than this. Here is the part of the file I want to "remember its state"
Thanks in advance.
public class Program { //Variable Table public static int FWAM = 0; public static int FWAM1C = 180; public static int FWAM2C = 300; public static int FWAM3C = 180; public static int FWAM1T = 35; public static int FWAM2T = 60; public static int FWAM3T = 35; public static int FWAM1A = 0; public static int FWAM2A = 1; public static int FWAM3A = 0; public static int FWAM1AA = 0; public static int FWAM2AA = 45; public static int FWAM3AA = 0; public static int FWAM1E = 0; public static int FWAM2E = 0; public static int FWAM3E = 0;
#13
Posted 13 August 2012 - 04:48 AM
Please Help.
using System; using System.IO; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using Microsoft.SPOT.IO; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.IO; using SecretLabs.NETMF.Hardware.NetduinoPlus; using System.Text; namespace ServerExample { public class SDCard { public static void CardRead() { using (var filestream = new FileStream(@"\SD\dontpanic.txt", FileMode.Create)) { StreamWriter streamWriter = new StreamWriter(filestream); streamWriter.WriteLine("This is a test of the SD card support on the netduino...This is only a test..."); streamWriter.Close(); } using (var filestream = new FileStream(@"\SD\dontpanic.txt", FileMode.Open)) { StreamReader reader = new StreamReader(filestream); Debug.Print(reader.ReadToEnd()); reader.Close(); } } } }
#14
Posted 13 August 2012 - 11:15 AM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users