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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
But why notepad and wordpad ... can open that?

Nicholas Paldino said:
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]
- (e-mail address removed)

MCH said:
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
 
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.
 
MCH said:
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.
 
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.

Forgot about that in the reader. The documentation implies that the
share mode only applies to subsequent opens, but it also applies to
already open handles as well...
 
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
 
MCH said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top