G
GB
Hi !
I have 2 different processes/application. One is writing to a file and
another is reading from it. For some reason the code doesnt seems to
work and gives mscorlib.dll IOException error "This file is being used
by another process".
Both the applications are in C#.
P.S. Even if I try to open the file with NotePad (while my server is
writing data in the file)it gives the same error.
Here's the code esnippet :
Writing the Buffer
********************
FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate,
FileAccess.Write, System.IO.FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
sw.Write(buff, 0, buff.GetLength(0)); // no. of bytes to write
Reading the Buffer
*********************
string FileName = "D:\\dataFile.txt";
StreamReader sr = new StreamReader(new
FileStream(FileName,FileMode.Open,FileAccess.Read));
int TotalBytesRead = 0;
int retval = sr.ReadBlock(mapBuff, 0, mapBuff.GetLength(0)); //bytes
read if(retval > 0)
{
TotalBytesRead = TotalBytesRead + retval;
}
Thread.Sleep(50);
Can anybody point out whats the error ? or is it a BUG ?
Thanks a lot for the help,
Gaurav
I have 2 different processes/application. One is writing to a file and
another is reading from it. For some reason the code doesnt seems to
work and gives mscorlib.dll IOException error "This file is being used
by another process".
Both the applications are in C#.
P.S. Even if I try to open the file with NotePad (while my server is
writing data in the file)it gives the same error.
Here's the code esnippet :
Writing the Buffer
********************
FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate,
FileAccess.Write, System.IO.FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
sw.Write(buff, 0, buff.GetLength(0)); // no. of bytes to write
Reading the Buffer
*********************
string FileName = "D:\\dataFile.txt";
StreamReader sr = new StreamReader(new
FileStream(FileName,FileMode.Open,FileAccess.Read));
int TotalBytesRead = 0;
int retval = sr.ReadBlock(mapBuff, 0, mapBuff.GetLength(0)); //bytes
read if(retval > 0)
{
TotalBytesRead = TotalBytesRead + retval;
}
Thread.Sleep(50);
Can anybody point out whats the error ? or is it a BUG ?
Thanks a lot for the help,
Gaurav