private static ReaderWriterLockSlim _rwl = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private static byte[] ReadFileData(string filename, int offset, int count)
{
byte[] buf = null;
try
{
_rwl.EnterReadLock();
using (FileStream filedata = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
if (count < 0)
count = (int)filedata.Length;
buf = new byte[count];
filedata.Seek(offset, SeekOrigin.Begin);
filedata.Read(buf, 0, count);
filedata.Close();
}
}
finally
{
if (_rwl.IsWriteLockHeld)
_rwl.ExitWriteLock();
}
return buf;
}
try
{
_rwl.EnterWriteLock();
if (!File.Exists(filename))
{
FileStream file = File.Create(filename);
file.Close();
}
if (Data.Length > 0)
{
using (FileStream file= new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
file.Seek(0, SeekOrigin.End);
file.Write(data, 0, data.Length);
file.Flush();
file.Close();
}
}
}
finally
{
_rwl.ExitWriteLock();
}