I had the same Problem with a log file funtion (also G120).
I could create only 3 files in that folder with my code.
I then delted the files, but after 3 files the 4ths was not there again (no Errors on writing).
I think that this had something to do that the files where not really/fully written to SD Card before I made a reset (I kept the file open und used flush on the TextWriter stream).
Then I changed my code to open and Close the file every time, and this Problem was solved.
But then it was so slow that I had to disable file logging fully (for now)
I ran a test on the G120, using the currently available firmware, and was able to create 279 files. This is where I stopped letting it run. Please run this code on your device to see if you still have an issue.
Last output:
==========================
Creating file #279
Writing out to file
Verifying file
Verification complete
==========================
FILE CONTENTS:
File279.txt -- This is file #279
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Microsoft.SPOT.IO;
using GHI.Premium.IO;
using System.IO;
namespace Topic_13912
{
public partial class Program
{
PersistentStorage sd;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
/*******************************************************************************************
Modules added in the Program.gadgeteer designer view are used by typing
their name followed by a period, e.g. button. or camera.
Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
button.ButtonPressed +=<tab><tab>
If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
timer.Tick +=<tab><tab>
timer.Start();
*******************************************************************************************/
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
RemovableMedia_Insert();
}
private void RemovableMedia_Insert()
{
Debug.Print("Insert event");
try
{
sd = new PersistentStorage("SD");
sd.MountFileSystem();
int file = 0;
while (true)
{
++file;
string outName = "File";
if (file < 10)
outName += "00" + file.ToString() + ".txt";
else if (file < 100)
outName += "0" + file.ToString() + ".txt";
else
outName += file.ToString() + ".txt";
Debug.Print("Creating file #" + file.ToString());
FileStream outstream = new FileStream("SD\\subfolder\\" + outName, FileMode.Create);
Debug.Print("Writing out to file");
string outtext = "This is file #" + file.ToString();
outstream.Write(System.Text.Encoding.UTF8.GetBytes(outtext), 0, outtext.Length);
outstream.Flush();
outstream.Close();
outstream.Dispose();
Debug.Print("Verifying file");
byte[] inbuffer = new byte[outtext.Length];
FileStream instream = new FileStream("SD\\subfolder\\" + outName, FileMode.Open);
int read = instream.Read(inbuffer, 0, outtext.Length);
if (read != outtext.Length)
{
Debug.Print("Length incorrect on file #" + file.ToString());
instream.Close();
instream.Dispose();
return;
}
string instring = new string(System.Text.Encoding.UTF8.GetChars(inbuffer));
if (instring != outtext)
{
Debug.Print("Byte mismatch on file #" + file.ToString());
instream.Close();
instream.Dispose();
return;
}
Debug.Print("Verification complete");
Debug.Print("==========================");
instream.Close();
instream.Dispose();
Thread.Sleep(100);
}
}
catch (Exception error)
{
}
}
}
}
Close Method:
Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.
It is not strict;y required, as I believe the implementation of dispose should call any methods such as close that a class requires before a destruction. Placement of both lines is purely habitual .