I am using the exact same code I used with a G400 board to write data to an USB flash disk. It’s also the same code I used with an SDcard
I can open the file and no errors but I get the following when I try to write to the disk.
A first chance exception of type ‘System.IO.IOException’ occurred in System.IO.dll
A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
This is the code and it is just writing a string to the file.
String filename = usbRootDirectory + "\\fuellevel.txt";
Boolean newFile = false;
file = new FileStream(filename, FileMode.Append);
file = new FileStream(filename, FileMode.Create);
newFile = true;
catch (Exception ex)
// File does not exist or error so we can't record data
Debug.Print("Failed to write log data");
StreamWriter write = new StreamWriter(file);
if (newFile) // Create header ?
logData = "DATE,TIME,LEVEL RAW,LEVEL FILTERED";
logData = GetLogTimeDate();
logData += "," + rawReading.ToString("F3");
logData += "," + filteredReading.ToString("F3");
I decided to switch to using the build in MicroSD card slot and it works with the same code above.
Something strange with the USB driver. The file is created but it never has anything in it.
I added a button to let the user unmount the SD card or USB flash before removing it with an LED to show when it can be removed. Using the USB nothing gets written but SD works every time.
To be safe, have you formatted the USB drive?
No as the drive has data already. I need this to work with a client disk. Asking them to format it is not idea. This is supposed to be designed a solution to copy test data to a USB disk.
I will find one with no data and give it a try to see if that works. Can’t see why a disk with existing data does not work. The file is there alright, just zero length.
Sometimes the filesystem can get into a corrupt state. Instead of formatting the disk, you can try and have Windows perform a scan disk on it and then test to see if it helps. If not and a formatted different drive works, then I’d try formatting this drive to test.
Reformatting the disk worked. Not an ideal situation as it means the user is forced to do this if there are any errors and he won’t know until he removes the disk and checks it later.
Where does that exception throw? On write or on close?
write for the first one and close the second.
Try getting the file stream this way;
System.IO.FileStream file = System.IO.File.OpenWrite("file.txt");
Or this way (used long form for namespace on both of these);
System.IO.FileStream filealt = new System.IO.FileStream("file.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Write);
I’m wondering if the directory check (exists) is creating a temporary lock on the file which isn’t clearing prior to writing data. NETMF docs say if the current pointer to a file changes unexpectedly you’ll get an exception on write; since it’s in the same Lock() if the Exist() call touches that file and hasn’t cleared prior to writing (which there might be some latency on USB file systems especially on a dirty FAT32 file system that’s been fragmented and written from a lot), then you might be trying to write before the lock from Exist releases.
Just stabbing at the dark here. But at least there’s a few other methods to try, and perhaps move the Exist() outside of the critical code section where you’re locking that section from outside processes?