Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

StreamReader locks the file

208 views
Skip to first unread message

fniles

unread,
Jun 16, 2008, 1:35:03 PM6/16/08
to
I am using VB.NET 2008 and StreamReader to read from a file on our server.
While the file is open in this VB.NET program, another program written in
VB6 at the same time trying to append to the same file, and it got
"Permission Denied" error.
I am wondering if I can use StreamReader without locking the file for update
by other program.

This is how I open the file
srBCAS = New StreamReader(m_sServer & "\log.txt",
System.Text.Encoding.Default)

Thank you


Herfried K. Wagner [MVP]

unread,
Jun 16, 2008, 5:14:54 PM6/16/08
to
"fniles" <fni...@pfmail.com> schrieb:

>I am using VB.NET 2008 and StreamReader to read from a file on our server.
> While the file is open in this VB.NET program, another program written in
> VB6 at the same time trying to append to the same file, and it got
> "Permission Denied" error.
> I am wondering if I can use StreamReader without locking the file for
> update by other program.

You have to allow shared access explicitly:

\\\
Dim Stream As New FileStream( _
"C:\foo.txt", _
FileMode.Open, _
FileAccess.Read, _
FileShare.ReadWrite _
)
Using Reader As New StreamReader(Stream)
...
End Using
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

kimiraikkonen

unread,
Jun 16, 2008, 7:11:06 PM6/16/08
to

I beleive you'd better use "Using" statement to do all the disposal
operation automatically, thus the underlying and all resources will be
freed.

Using srBCAS As New StreamReader(m_sServer & "\log.txt",
System.Text.Encoding.Default)

' Code...Typically srBACS.ReadToEnd()

End Using

Hope this helps,

Onur Güzel

Göran Andersson

unread,
Jun 17, 2008, 1:45:10 PM6/17/08
to

That is helpful for making sure that the file is properly closed when
you are done with it.

However, that is not the main problem here. The OP want's one process to
be able to write to the file while another process is still reading it.

As already mentioned, the solution is to specify the sharing mode when
opening the stream.

--
Göran Andersson
_____
http://www.guffa.com

fniles

unread,
Jun 19, 2008, 2:03:19 PM6/19/08
to
Thank you. That fixed my problem.
Thanks again

"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message
news:Oe7NKY$zIHA...@TK2MSFTNGP02.phx.gbl...

0 new messages