I’ve this simple code:
using(FileStream fs = new FileStream(@"E:\test.bin", FileMode.Open, FileAccess.Read, FileShare.Read)
{
byte[] bytes = new byte[100];
int n = fileStream.Read(bytes, 0, bytes.Length);
}
that normally works as expected returning the array of bytes at the beginning of the file.
Incredibly when I try to read a quite large file (~7GB) located on an external HD connected through USB, the bytes in the returned array are all zeros and n is 100.
If I try a file of the same type but smaller everything works fine. If I try the same big file on another external HD I have, again everything is fine. Even more incredibly, if I try the same big file and I use the same external HD that gives me problems but connected through eSATA everything works fine!
Both HDs are NTFS and the described behavior on the problematic HD is not 100% stable: sometimes files around 7GB are not read, sometimes they are; files around 10GB are never read.
Any idea on what can cause this and how to solve it?
EDIT:
I’ve tried by reading 1GB of data and nothing changes. But here there are other hints: if the file is too large the time for reading 1GB of bytes is too short to be a real reading; moreover if I wait the HD to go in standby mode and if I try to read 1GB from a small file the HD exits standby, the reading takes time and succeeds, while if I try with a large file the HD does not exit the sleep mode, the reading takes few milliseconds and again all zeros.
If I initialize the byte array with some values, those values are erased after the “fake” reading and again I’ve all 0’s.
Given the fact that same code work fine with some drives, and you have no control on how your drive works thru USB or whatever, I guess it’s just a hardware problem. Try to run program with the same drives on another computer.