Home All Groups Group Topic Archive Search About

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

Author
9 Oct 2006 4:34 PM
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

Author
9 Oct 2006 4:41 PM
GS
what is your code for opening the file?
Did you open for read only?

Show quoteHide quote
"Robinson" <toomuchspamhaspassed@myinboxtoomuchtoooften.com> wrote in
message news:egdtn3$5jq$1$8300dec7@news.demon.co.uk...
> 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
>
>
Author
9 Oct 2006 4:53 PM
Robinson
"GS" <gsmsnews.microsoft.co***@msnews.Nomail.com> wrote in message
news:eWPB0G86GHA.4644@TK2MSFTNGP04.phx.gbl...
> 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
Author
9 Oct 2006 4:58 PM
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.

Show quoteHide quote
"Robinson" <toomuchspamhaspassed@myinboxtoomuchtoooften.com> wrote in
message news:egduqq$79n$1$8300dec7@news.demon.co.uk...
>
> "GS" <gsmsnews.microsoft.co***@msnews.Nomail.com> wrote in message
> news:eWPB0G86GHA.4644@TK2MSFTNGP04.phx.gbl...
>> 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
>
Author
9 Oct 2006 10:15 PM
Robinson
"Marina Levit [MVP]" <someone@nospam.com> wrote in message
news:eclAoQ86GHA.4592@TK2MSFTNGP04.phx.gbl...
> 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
Author
9 Oct 2006 11:41 PM
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

Show quoteHide quote
> "Marina Levit [MVP]" <someone@nospam.com> wrote in message
> news:eclAoQ86GHA.4592@TK2MSFTNGP04.phx.gbl...
>
>> 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
>
Author
11 Oct 2006 9:51 AM
Robinson
"GhostInAK" <p***@paco.net> wrote in message
news:be1391bf1beff8c8b9d07d43785c@news.microsoft.com...
> 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)
Author
10 Oct 2006 1:37 AM
teslar91
Robinson wrote:
> 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)