FileShare.Read error ?

  • Thread starter Thread starter GB
  • Start date Start date
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
 
GB said:
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#.

I'd try it in the C# group: ;-) This is the VB.Net group.
microsoft.public.dotnet.languages.csharp


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hi GB,

Did you send this with a reason to the language VB group or by accident?

However a file using the streamreader/writer will be locked until it is
closed.

Cor
 
* "Cor Ligthert said:
Did you send this with a reason to the language VB group or by accident?

However a file using the streamreader/writer will be locked until it is
closed.

Not necessarily:

\\\
Dim fs1 As New System.IO.FileStream("C:\Log.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim fs2 As New System.IO.FileStream("C:\Log.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim sr1 As New System.IO.StreamReader(fs1)
Dim sr2 As New System.IO.StreamReader(fs2)
MsgBox(sr1.ReadLine())
MsgBox(sr2.ReadLine())
sr1.Close()
sr2.Close()
///
 
Hi Herfried,

Did not look that well, you know I do not like to give a redirect answer
when I have the idea that I can give an answer.

Thank you for the message, keeps me attent..

Cor
 
* "Cor Ligthert said:
Did not look that well, you know I do not like to give a redirect answer
when I have the idea that I can give an answer.

Thank you for the message, keeps me attent..

I didn't know that too, before testing it. Then I made a typo and typed
'fs1' instead of 'fs2' and thought that reading the same file using 2
streamreaders didn't work. I think I should take more sleep.
 
Back
Top