Hi! Just want to clarify something about Reading/Writing of bytes in the FEZ.
I’m not sure if it’s only a .NETMF or a Fez issue so I’ll post here also.
Problem:
When I read and write a File(.exe, .doc) in a USB flash drive connected to the Fez ( to duplicate the File), the copied File is corrupt(MD5 check fail). But the File is in correct File Size as the original.
I’ve tried the SAME code on .NET C# and .NETMF C#.
C# .NET
using System;
using System.Windows.Forms;
using System.IO;
class MainClass
{
public static void Main()
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Title="Select file to back up";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
FileStream inStream = File.OpenRead(dlgOpen.FileName);
FileStream outStream = File.OpenWrite(dlgOpen.FileName + ".bak");
int b;
while ((b = inStream.ReadByte()) > -1)
outStream.WriteByte( (byte) b);
outStream.Flush();
outStream.Close();
inStream.Close();
}
}
}
C# .NETMF
using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
using System.IO;
using System;
namespace MFConsoleApplication1
{
class Program
{
static PersistentStorage ps;
static void DeviceConnectedEvent(USBH_Device device)
{
if (device.TYPE == USBH_DeviceType.MassStorage)
{
Debug.Print("USB Mass Storage detected...");
ps = new PersistentStorage(device);
ps.MountFileSystem();
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
FileStream inStream = new FileStream(rootDirectory + @ "\sample.docx",FileMode.Open, FileAccess.Read);
FileStream outStream = new FileStream(rootDirectory + @ "\5kbcopy.docx", FileMode.OpenOrCreate, FileAccess.Write);
int b;
while ((b = inStream.ReadByte()) > -1)
outStream.WriteByte((byte)b);
outStream.Flush();
outStream.Close();
inStream.Close();
Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
public static void Main()
{
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
}
Another code
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
try
{
using (FileStream fsSource = new FileStream(rootDirectory + @ "\longtext.txt",
FileMode.Open, FileAccess.Read))
{
// Read the source file into a byte array.
byte[] bytes = new byte[fsSource.Length];
int numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = bytes.Length;
// Write the byte array to the other FileStream.
using (FileStream fsNew = new FileStream(rootDirectory + @ "\superlong.txt",
FileMode.Create, FileAccess.Write))
{
fsNew.Write(bytes, 0, numBytesToRead);
}
}
}
catch
{
}
I receive no errors when running it on the FEZ. Also, I thing I’ve noticed on the FEZ, when I ADD a code to display the READ BYTES on the debug console for the FEZ(.NETMF) and also on a textbox for the .NET, the displayed BYTES from the FEZ are different from the .NET.