473,396 Members | 2,154 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,396 software developers and data experts.

does StreamReader need exclusive access to the file?

I'm using StreamReader to read a text file and sometimes I get an error
saying that the file is opened by someone else.

I haven't found any info about how to set a flag to tell StreamReader to
open the file in shared mode and read mode.

Thanks!
Daniel

Nov 15 '05 #1
4 7979
Hi Daniel,

Strema reader uses new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.Read) if you don't pass the already stream in the constructor.
You might pass a valid stream object in the constructor of StreamReader if
you feel like opening the file differently.
And, don't forget to close it.
--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Daniel P." <da******@hotmail.comU> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
I'm using StreamReader to read a text file and sometimes I get an error
saying that the file is opened by someone else.

I haven't found any info about how to set a flag to tell StreamReader to
open the file in shared mode and read mode.

Thanks!
Daniel

Nov 15 '05 #2
Daniel,

In this case, you will want to use the FileStream object and open the
file yourself with the appropriate access. Once you have that, you can pass
the FileStream instance to the StreamReader constructor.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel P." <da******@hotmail.comU> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
I'm using StreamReader to read a text file and sometimes I get an error
saying that the file is opened by someone else.

I haven't found any info about how to set a flag to tell StreamReader to
open the file in shared mode and read mode.

Thanks!
Daniel

Nov 15 '05 #3
Thanks Nicholas!
Thanks Miha!

Do I close both the StreamReader and FileStream?

If I use
using( ... )
{
}

do I still have to close it or Dispose will close the file automatically?

Thanks!
"Miha Markic" <miha at rthand com> wrote in message
news:O4****************@TK2MSFTNGP12.phx.gbl...
Hi Daniel,

Strema reader uses new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.Read) if you don't pass the already stream in the constructor.
You might pass a valid stream object in the constructor of StreamReader if
you feel like opening the file differently.
And, don't forget to close it.
--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Daniel P." <da******@hotmail.comU> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
I'm using StreamReader to read a text file and sometimes I get an error
saying that the file is opened by someone else.

I haven't found any info about how to set a flag to tell StreamReader to
open the file in shared mode and read mode.

Thanks!
Daniel


Nov 15 '05 #4
Hi Daniel,

I think that closing StreamReader is enough.
Also, "using" on streamreader is enough.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Daniel P." <da******@hotmail.comU> wrote in message
news:OW**************@TK2MSFTNGP09.phx.gbl...
Thanks Nicholas!
Thanks Miha!

Do I close both the StreamReader and FileStream?

If I use
using( ... )
{
}

do I still have to close it or Dispose will close the file automatically?

Thanks!
"Miha Markic" <miha at rthand com> wrote in message
news:O4****************@TK2MSFTNGP12.phx.gbl...
Hi Daniel,

Strema reader uses new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.Read) if you don't pass the already stream in the constructor.
You might pass a valid stream object in the constructor of StreamReader if you feel like opening the file differently.
And, don't forget to close it.
--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Daniel P." <da******@hotmail.comU> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
I'm using StreamReader to read a text file and sometimes I get an error saying that the file is opened by someone else.

I haven't found any info about how to set a flag to tell StreamReader to open the file in shared mode and read mode.

Thanks!
Daniel



Nov 15 '05 #5

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

Similar topics

4
by: cody | last post by:
When using a StreamReader I cannot open files that are currently opened from another application and are therefore readonly. But I can open these files in notepad. It seems that StreamReader tries...
1
by: Chuck Van Den Corput | last post by:
I have encountered a problem that I am hoping someone can shed some light on. I have a multi-user A97 app. All users have a personal front-end MDE accessing a shared back-end. There is no...
18
by: Andre Laplume via AccessMonster.com | last post by:
I have inherited a bunch of dbs which are are shared among a small group in my dept. We typically use the dbs to write queries to extract data, usually dumping it into Excel. Most dbs originated...
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...
4
by: Leonardo Hyppolito | last post by:
Hello, I am trying to write a multithread program that simulates producers and consumers. My program can have many producers and many consumers (each in a separate thread). It has a storage...
1
by: ext237 | last post by:
Hello. I am trying to open a filestream using the following code: Dim myStreamReader As StreamReader = New StreamReader( _ New FileStream("H:\files\file.txt", FileMode.Open), _ ...
1
by: PJ | last post by:
Hi all, I have two option to open a file stream 1. Dim mStream As Stream = New FileStream(localFile, FileMode.Open, FileAccess.Read) This works fine as long as the file is closed. If a file...
1
by: CJJ | last post by:
hi Folks, I am opening my access database file (Access 2003) from a C# application. The connection string specifies mode=Share Exclusive The intent is that the application have exclusive...
2
by: ARC | last post by:
Hello all, When a user starts my app, I run re-attaching code if the links are not valid, etc. In this routine, I then check the version of the back-end database. If the version is off, I run...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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
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
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.