FileShare.Read error ?

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
 
A

Armin Zingler

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
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

* "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()
///
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

* "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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

FileShare.Read error ? 2
streamreader problem 2
HELP Please!!!! 3
file transfer help? 2
Streaming files 4
Streamwriter removes french characters 4
File To Stream - Stream To File 3
Stream issue 4

Top