473,387 Members | 1,603 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,387 software developers and data experts.

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

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
Oct 9 '06 #1
7 2174
GS
what is your code for opening the file?
Did you open for read only?

"Robinson" <to******************@myinboxtoomuchtoooften.comwr ote in
message news:eg*******************@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


Oct 9 '06 #2

"GS" <gs**********************@msnews.Nomail.comwrote in message
news:eW**************@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
Oct 9 '06 #3
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.

"Robinson" <to******************@myinboxtoomuchtoooften.comwr ote in
message news:eg*******************@news.demon.co.uk...
>
"GS" <gs**********************@msnews.Nomail.comwrote in message
news:eW**************@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

Oct 9 '06 #4

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:ec**************@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
Oct 9 '06 #5
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
"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:ec**************@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

Oct 9 '06 #6

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)

Oct 10 '06 #7

"GhostInAK" <pa**@paco.netwrote in message
news:be**************************@news.microsoft.c om...
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)
Oct 11 '06 #8

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

Similar topics

11
by: David Morgenthaler | last post by:
How does one overide the iterator implied by the construct "for line in file:"? For example, suppose I have a file containing row,col pairs on each line, and I wish to write a subclass of file...
9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
2
by: Henrik | last post by:
Hi Im trying to make an app that uses threading, I want to write to a file in a loop. How can i ensure that only one thread writes to the file a the time??? Im accessing the fil with: ...
2
by: FrzzMan | last post by:
The first time I called this function, everything went well, but the second time I called it. An Exception thrown, do you know why? An unhandled exception of type 'System.IO.IOException' occurred...
3
by: kris.dorey | last post by:
Hi, Ive got the following code which seems ok but when the user runs the function for a second time I get an error message stating that the mdb is in use by another process. There is still an...
6
by: John Doe | last post by:
Hi, I've got the following error when building my ASP.NET application called WebApplication1: Preparing resources... Updating references... Performing main compilation... error CS0006:...
7
by: RSH | last post by:
Hi, I have an ASP .Net web page that creates a temp directory on the server then it is using the File.Copy command to move a file to a temp download directory, Then I am doing a...
4
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.