Hi,
I'm troubleshooting an issue in the field where a simple text file that gets
read/written over and over again ends up zeroed out somehow (ie it should
have XML, but when looked at in a binary editor it just has 00 00 00 ...).
This is being reported by a handful of customers, but we have been unable to
reproduce it.
The following is the simplified code. There are clearly some curious and
suspicious choices, but the key is that we're not getting exceptions .. just
the corrupt file. Does anyone have any theories - either about this code or
other utilities on a system that might be causing this? I'd love to actually
reproduce this before refactoring.
string _file = @"C:\ProgramData\Data.txt";
[Test]
public void Test_TryToReproduceProblem() {
string record = GenerateRecord(); //creates variable xml
using (FileStream fs = ReadFile())
{
fs.SetLength(0);
fs.Position = 0;
StreamWriter streamWriter = new StreamWriter(fs);
streamWriter.Write(record);
streamWriter.Flush();
}
Assert.AreEqual(record, File.ReadAllText(_file));
}
private FileStream ReadFile() {
FileStream fs = new FileStream(_file,
FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.None);
StreamReader streamReader = new StreamReader(fs);
while (true)
{
string line = streamReader.ReadLine();
if (line == null)
{
break;
}
//REMOVED: Do something with line
}
return fs;
}
Note this happens with both .Net 1.1 and 2.0.
Thanks,
Orlando