The Netduino forums have been replaced by new forums at community.wildernesslabs.co.
This site has been preserved for archival purposes only
and the ability to make new accounts or posts has been turned off.
The full .NET Framework will ensure that a FileStream object is disposed of by any wrapper objects that get created (e.g. StreamWriter etc). Because of this, it isn't necessary to call dispose (via a using statement) on a wrapped FileStream since the wrapper will do it for you. Of course, in the interests of readability and consistency it is good practice to always explicitly declare your intentions on a FileStream with either a using statement or a try/finally block. However, I'm not sure if NETMF behaves the same way as its big brother on this.
At any rate, it appears that instead of your StreamWriter instance wrapping your FileStream object both are wrapping the shared file path. This indicates to me you have two Stream derived objects competing for the same resource at the same time.
Try this:
using (FileStream file = new FileStream(fileWrite, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter wr = new StreamWriter(file))
{
wr.WriteLine("Hello");
wr.WriteLine("StreamWriter");
}
I do not know what is going on, but i hate this "Magic". After several(approximately 100 (4 hours)) times of debugging application, it can some time work, some time did not work.Deployment Erasing and VS rebooting can some time help, some time no. I wasted a lot of time ... and i cant find any logic dependencies.
I do not know what is going on, but i hate this "Magic". After several(approximately 100 (4 hours)) times of debugging application, it can some time work, some time did not work.Deployment Erasing and VS rebooting can some time help, some time no. I wasted a lot of time ... and i cant find any logic dependencies.
And again Reader works fine
Default .Net application with SD Card works fine.
Exactly the same story with Netduino 3 WiFi today. Got two 2GB micro-SD cards, could read from both PC and Netduino, could not write to Netduino. Suddenly, after 2 hours of debugging and trying to figure out the problem it started to work.
My only guess that I can connect to is that either after the first usage of StreamReader (before that I was using just Directory.GetDirectories() and Directory.GetFiles() to get the list of directories and files) or File.Exists() it triggered the magic of being able to write files.
I tried to undo the magic by reformatting the card, erasing Netduino from Deployment Tool, restarting Visual Studio, but the magic remains.
public class Program
{
private const string FileName = @"\SD\FromND.txt";
public static void Main()
{
WriteTestFile();
ReadDirectoriesAndFiles();
ReadTestFile();
}
private static void WriteTestFile()
{
if (File.Exists(FileName))
{
File.Delete(FileName);
}
using(var streamWriter = new StreamWriter(FileName, false))
{
streamWriter.WriteLine("This is the read line from netduino file on microSD card " + FileName);
streamWriter.Flush();
streamWriter.Close();
}
}
private static void ReadTestFile()
{
using(StreamReader reader = new StreamReader(FileName))
{
string allText = reader.ReadToEnd();
reader.Close();
Debug.Print(allText);
}
}
public static void ReadDirectoriesAndFiles()
{
Debug.Print("Directories and Files: ");
foreach(var directoryName in Directory.GetDirectories(@"\"))
{
Debug.Print(directoryName);
foreach(var fileName in Directory.GetFiles(directoryName))
{
Debug.Print(fileName);
}
}
}
}
so, it seems to be working now. It does not feel "stable" but I will post if the magic disappears and the problem comes back.