473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

StreamReader locks the file

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
Jun 27 '08 #1
4 10736
"fniles" <fn****@pfmail.comschrieb:
>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/>

Jun 27 '08 #2
On Jun 16, 8:35*pm, "fniles" <fni...@pfmail.comwrote:
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
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
Jun 27 '08 #3
kimiraikkonen wrote:
On Jun 16, 8:35 pm, "fniles" <fni...@pfmail.comwrote:
>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

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
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
Jun 27 '08 #4
Thank you. That fixed my problem.
Thanks again

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:Oe**************@TK2MSFTNGP02.phx.gbl...
"fniles" <fn****@pfmail.comschrieb:
>>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/>

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
9
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
1
by: Tarren | last post by:
Hi: Question which might have something to do with encoding? I have a text file and I use the following code to iterate through. StreamReader oStreamFile = new StreamReader(sFileName); ...
0
by: Dave | last post by:
I have a static Crystal reports .rpt file that I access via the Report Viewer in ASP.NET. When I access it on the webpage, even after I exit the browser and stop viewing the .rpt file, it will not...
0
by: Taiwo | last post by:
When I use the method "System.Drawing.Image.FromFile(pictureFile)" where pictureFile is the path to a valid image file, the file is locked even minutes after the statement executes. This code is in...
2
by: Steve Bugden | last post by:
Hi, I have an asp.net application running under the .net framework 2.0. The application pool locks any image files if recently displayed in the browser. So I am unable to update any of these...
0
by: Slawomir Nowaczyk | last post by:
Hello, When I execfile a file which contains a syntax error, the file becomes locked and stays this way all the way until I exit the interpreter (I am unable to delete it, for example). I have...
3
by: Tom | last post by:
I don't want to re-invent the wheel and am looking for a simple implementation of a text viewer or RichTextBox in read only mode that allows rapid file positioning within large data files without...
1
by: PSiegmann | last post by:
Hello group, if I open with streamreader a file to read in that way: StreamReader sr = new StreamReader(c:\\test.txt, System.Text.Encoding.GetEncoding(10000)); Is it possible that it can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.