Something I don't quite understand about "file in use by another process" exceptions.....

R

Robinson

Hi,

I was just playing around with my log files and tried to open a log file
programmatically that was considered "in use" (I got an in-use exception).
The file is being used by my debug writer and I wanted to read lines from
the file on one of my forms. It's a simple text file by the way. Now
obviously the exception I get from the code trying to open the file for
reading looks like this:

The process cannot access the file 'C:\Documents and
Settings\mymachine\Application Data\mycomapny\myproduct\log.txt' because it
is being used by another process.

What I don't understand is that I can double click the file and have it open
up in notepad.exe. I don't get an "in use" exception from Explorer, so what
is notepad.exe doing that I cannot being able to open the file? I notice
that you can File.Copy the file while it's in-use and this strikes me as one
way I could get to read a file already in use by the system (copy and then
open and read the copy). Can anyone shed any light on Windows inner
workings in this respect?

Thanks,



Robin
 
R

Robinson

GS said:
what is your code for opening the file?
Did you open for read only?

Hi,

Yes, I tried File.ReadAll (as a test), a StreamReader and File.Open ( for
reading ). All of these threw the exception, but File.Copy did not.


Robin
 
M

Marina Levit [MVP]

That was not the question.

The question was whether or not you specified that you are opening the file
in read-only mode when opening the file for reading.
 
R

Robinson

Marina Levit said:
That was not the question.

The question was whether or not you specified that you are opening the
file in read-only mode when opening the file for reading.

File.ReadAllLines does not have an overload to specify for read-only, just
path and encoding.

File.Open, has Create, Append, CreateNew, Open, OpenOrCreate, Truncate (I
chose Open)

Dim theStream As New StreamReader, has overloads for encoding and
auto-detection of encoding, but not a "read only" flag. However in the
latter case, I'm sure it's read only in any case because it's a
streamreader, not a streamreaderorwriter.

Did I miss something here?



Robin
 
G

GhostInAK

Hello Robinson,

I've not validated your assertions, but the FileStream class does provide
an Open method that takes a (I believe) FileMode enum (flag) value.

-Boo
 
T

teslar91

Robinson said:
File.ReadAllLines does not have an overload to specify for read-only, just
path and encoding.

File.Open, has Create, Append, CreateNew, Open, OpenOrCreate, Truncate (I
chose Open)

Dim theStream As New StreamReader, has overloads for encoding and
auto-detection of encoding, but not a "read only" flag. However in the
latter case, I'm sure it's read only in any case because it's a
streamreader, not a streamreaderorwriter.

Hi Robin,

You mentioned trying to understand the inner workings. All of these
..NET framework methods are just indirect means to call the Windows API.

And when opening a file directly with the API, in addition to the
filename it expects three parameters:

FileMode: Create, Append, CreateNew, Open, OpenOrCreate, Truncate
FileAccess: Read, Write, ReadWrite
FileShare: Delete, Inheritable, None, Read, ReadWrite, Write

I don't know my way around the .NET framework yet as well as the API,
but it seems that the problem is that the methods you've tried so far
assume FileShare=None, and do not allow you to override it to
ReadWrite, which is what you really need. But I do know of one that
does (there may be more):

Dim f As New System.IO.FileStream("c:\log.txt", IO.FileMode.Open,
IO.FileAccess.Read, IO.FileShare.ReadWrite)
 
R

Robinson

GhostInAK said:
Hello Robinson,

I've not validated your assertions, but the FileStream class does provide
an Open method that takes a (I believe) FileMode enum (flag) value.

-Boo

Thanks for all above replies, I did indeed miss an overload when searching
the documentation. The correct way of doing it is:

theStream = File.Open(Application.UserAppDataPath + "\log.txt",
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
 

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

Top