473,460 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Create 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.IOException 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 10255
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(new 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
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...
1
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...
21
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...
2
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...
7
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...
2
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...
13
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...
5
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...
2
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...
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
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.