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

using OpenText() to open a file that is opened(locked) by others

I try to open a log file that is logging infomation by another process:

StreamReader sr = File.OpenText(filePath);

I got the exception message:

The process cannot access the file 'c:\temp\myLog.txt' because it is being
used by another process.

How can I open, read and process the file in C#?

Thanks

MCH
Sep 4 '07 #1
6 9243
MCH,

The StreamReader constructor is creating a FileStream instance which can
read a file. Whatever is writing to that file at the time has made it so
that you can not read it while it has a handle open to it. You have to stop
the program that is accessing it, or you have to change it so that it allows
read access while it is being used.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MCH" <MC*@discussions.microsoft.comwrote in message
news:D8**********************************@microsof t.com...
>I try to open a log file that is logging infomation by another process:

StreamReader sr = File.OpenText(filePath);

I got the exception message:

The process cannot access the file 'c:\temp\myLog.txt' because it is being
used by another process.

How can I open, read and process the file in C#?

Thanks

MCH

Sep 4 '07 #2
But why notepad and wordpad ... can open that?

"Nicholas Paldino [.NET/C# MVP]" wrote:
MCH,

The StreamReader constructor is creating a FileStream instance which can
read a file. Whatever is writing to that file at the time has made it so
that you can not read it while it has a handle open to it. You have to stop
the program that is accessing it, or you have to change it so that it allows
read access while it is being used.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MCH" <MC*@discussions.microsoft.comwrote in message
news:D8**********************************@microsof t.com...
I try to open a log file that is logging infomation by another process:

StreamReader sr = File.OpenText(filePath);

I got the exception message:

The process cannot access the file 'c:\temp\myLog.txt' because it is being
used by another process.

How can I open, read and process the file in C#?

Thanks

MCH


Sep 4 '07 #3
On Sep 4, 2:50 pm, MCH <M...@discussions.microsoft.comwrote:
I try to open a log file that is logging infomation by another process:

StreamReader sr = File.OpenText(filePath);

I got the exception message:

The process cannot access the file 'c:\temp\myLog.txt' because it is being
used by another process.

How can I open, read and process the file in C#?

Thanks

MCH
Whatever's doing the logging needs to ensure that the log file is
opened with FileShare.Read. Otherwise, the file is locked from all
other processes. IIRC, the default TextWriterTraceListener does
this. However, if you are rolling your own logging, you need to
ensure this occurs.

Sep 4 '07 #4
"MCH" <MC*@discussions.microsoft.comwrote in message
news:83**********************************@microsof t.com...
But why notepad and wordpad ... can open that?
You can't use File.OpenText for this, OpenText tries to open the file in
"shared read" mode which fails if another application/thread has the file
open for writing. You'll have to use the FileStream class and open the file
for reading with sharing mode set to ReadWrite.

Willy.
Sep 4 '07 #5
Thanks you for your responce.

The problem is, I cannot control the log file. It's not written by me. It's
obviously locked. I only want to do is, like notepad, open the file, read and
get usable infomation.. by C#.

Thanks

MCH

"Willy Denoyette [MVP]" wrote:
"MCH" <MC*@discussions.microsoft.comwrote in message
news:83**********************************@microsof t.com...
But why notepad and wordpad ... can open that?

You can't use File.OpenText for this, OpenText tries to open the file in
"shared read" mode which fails if another application/thread has the file
open for writing. You'll have to use the FileStream class and open the file
for reading with sharing mode set to ReadWrite.

Willy.
Sep 4 '07 #6
"MCH" <MC*@discussions.microsoft.comwrote in message
news:23**********************************@microsof t.com...
Thanks you for your responce.

The problem is, I cannot control the log file. It's not written by me.
It's
obviously locked. I only want to do is, like notepad, open the file, read
and
get usable infomation.. by C#.
You don't need to "control the log file", you need to use the FileStream
class and open the file in shared readwrite mode instead of using
File.OpenText .

using(FileStream fileStream = new FileStream(
"yourLogfile", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
....

Willy.

Thanks

MCH

"Willy Denoyette [MVP]" wrote:
>"MCH" <MC*@discussions.microsoft.comwrote in message
news:83**********************************@microso ft.com...
But why notepad and wordpad ... can open that?

You can't use File.OpenText for this, OpenText tries to open the file in
"shared read" mode which fails if another application/thread has the file
open for writing. You'll have to use the FileStream class and open the
file
for reading with sharing mode set to ReadWrite.

Willy.

Sep 5 '07 #7

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

Similar topics

4
by: Albert Greinöcker | last post by:
Hi ng, I have a very strange problem within my c# application: One of the (background) Threads in my application is responsible for some image processing tasks (like resizing, clipping, ..) on a...
8
by: Lieve | last post by:
Hello, I set up a database a few months ago and placed it on our company network. In the beginning, there was no problem opening it, even when someone else was working in it at that time. The...
1
by: MrB | last post by:
I have an asp.net app that uses an access .mdb file. I want to periodically update this file via ftp. However when I try I run into the problem that the original mdb file can't be deleted because...
0
by: Keith | last post by:
Hi. I've developed an ASP.NET web service that uses an xml file to store configuration settings. I'm creating a cache dependency based on the xml file and its schema, like this... XmlDocument...
3
by: thebhone | last post by:
I am creating a website updater program. Currently working on the image replacement module here. For some reason, after an image (say /images/1.jpg) has been used/opened in the application. I...
1
by: Joe | last post by:
any other way than Try / Catch / Finally ??
2
by: Smokey Grindle | last post by:
I need a way to tell if a program such as word or excel has a document it locked (open for editing). Is there any way to do this in VB.NET? All I need is a its open for editing or not response...
5
by: buu | last post by:
I have an app made in vb.net with a part of it made in vc.net. after finishing a data processing with a file, I would like to delete that file, but file remains locked to be sure, I was: -...
1
by: boss306 | last post by:
Unable to undo checkout .asp file locked by X:user Ms frontpage 2003 main index.asp file is checked out and locked for editting by a user in which is no longer with company. Could anyone point me...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.