473,804 Members | 3,802 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamReader: Avoiding IOException due to external lock

I am reading a text file using StreamReader. If while I have that stream
open I open the underlying text file in, say, Excel, Excel will announce that
the file is locked for reading and will offer to open it in Read-Only mode.

But if I do that, then my StreamReader immediately throws a
System.IO.IOExc eption announcing, "The process cannot access the file because
another process has locked a portion of the file."

How can I access a file with a StreamReader in such a way that the file is
locked for reading AND it doesn't fail if another process also tries to
access the file in read-only mode?

Jul 11 '06 #1
1 10280
Hi

This depends on how the file was opened by another process. If this file
has already been opened exclusively, it cannot be opened by StreamReader
anyway. This is defined by the operating system.

However, if this file is opened as read-only, you can use a try block to
test for the IOException and in the catch block open it as read-only with
the following code.

StreamReader sr = new StreamReader(ne w FileStream(@"c: \test1.xls",
FileMode.Open, FileAccess.Read , FileShare.Read) );

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jul 12 '06 #2

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

Similar topics

14
3047
by: Eloff | last post by:
This is not really Python specific, but I know Python programmers are among the best in the world. I have a fair understanding of the concepts involved, enough to realize that I would benefit from the experience of others :) I have a shared series of objects in memory that may be > 100MB. Often to perform a task for a client several of these objects must be used. Since many clients can be making requests at once (>100 per second during...
1
1572
by: some guy | last post by:
Note: This is happening under the VS05 February CTP release. I haven't had a chance to run it under Beta 2 yet. I have a StreamReader that is reading a NetworkStream from a TcpClient, which is connected to a TcpListener. The client (the one that throws the exception later) can send information over the stream, but the client times out waiting for the response from the server - I get a System.IO.IOException (Unable to read data from the...
21
13110
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
2
2235
by: cagey cretin | last post by:
I have set up a catch/try deal to find an error, but there are none reported nor when I clean the build/rebuild. Simple filecalled text_file.txt. I cannot write to it, but I can write into it with notepad and read it with StreamReader. Can anyone show me the error of my ways? //AppendText() Method #using <mscorlib.dll> using namespace System;
7
2018
by: Daniel | last post by:
I have an winform application that uses System.IO.StreamReader and Serializer.Deserialize to load some data from a file, if any error is detected in the file a new file should be written to the same directory, I use a StreamWriter and Serializer.Serialize to do this. The problem is that when the streamreader has opened the file it does not release it until the application is closed. When I try to use the StreamWriter to write the new...
2
5379
by: Kin | last post by:
I am trying to read the output of an external application using redirected stdout and StreamReader::ReadLine(). The problem is that ReadLine() blocks and I am either reading nothing or just part of the output. I start a new process from my app to run the external app. StartInfo.RedirectStandardOutput is set to true. Then I start a thread that just keep reading the standard output of this new process in an infinite loop using...
13
11159
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
5
7606
by: jkristia | last post by:
StreamReader throws an exception if I attempt to open a .csv file which is also opened by Excel. I checked this list and found what I though was the solution, first open the file stream in read only, But unfortunately that doesn't work either Stream s = File.OpenRead(filename); using (StreamReader sr = new StreamReader(s)) { }
2
1897
by: asem0525 | last post by:
I have this kind of exception: System.IO.IOException: The process cannot access the file 'E:\asem\Visual Studio 2005\WebSites\WebSite1\App_Data\Books.txt' because it is being used by another process. and don't know the solution. Here is the code, somebody help me please, and thank you in advance. protected void AddBook() { string strISBN = tbISBN.Text; string filePath = Server.MapPath("Books.txt");
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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
10075
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...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5519
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
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
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.