473,671 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7993
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******@hotma il.comU> wrote in message
news:ue******** ******@TK2MSFTN GP10.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.co m

"Daniel P." <da******@hotma il.comU> wrote in message
news:ue******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP12.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******@hotma il.comU> wrote in message
news:ue******** ******@TK2MSFTN GP10.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******@hotma il.comU> wrote in message
news:OW******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP12.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******@hotma il.comU> wrote in message
news:ue******** ******@TK2MSFTN GP10.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
1574
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 to open the file in writemode which is then denied an an exception occures.
1
3252
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 specific MDW file for the app as security rules are housed within the app itself. On a couple of occasions, different users have launched the app only
18
7331
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 in MsAccess 97 or prior and have been converted to 2003. On occassion user 1 will open a db. When user 2 opens the db it will not let user 2 modify macros and what not. I can understand this and realize we could split the db; it is not worth ...
1
26739
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 byte conversion ?? FileStream stream = null; stream = File.Open(fiPath, FileMode.Open, FileAccess.ReadWrite,FileShare.None); System.IO.StreamReader Tin = new System.IO.StreamReader (stream);
4
1975
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 place (buffer) with "n" capacity. The user provides these parameters on startup. The buffer works with a circular queue. It's a console application. My implementation is working with Semaphores to synchornize everything. I think it's synchornized...
1
1833
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), _ System.Text.Encoding.Unicode)
1
937
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 is being opened by another process (even a .log or .txt file), it fails.
1
3678
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 access to the database file. No other instance of MS Access or this particular C# application (or any other app) should be able to open the access database file concurrently.
2
6220
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 code that then upgrades their back-end db by adding new fields, etc. However, if new fields are needed, I do run a function that I wrote that is supposed to determine if there are other user's in the back-end database. If so, it tells them to...
0
8914
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8820
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8670
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7433
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5695
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4224
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2810
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.