473,394 Members | 2,160 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 10735
"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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.