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

Checking if file being accessed

I am trying to get access to a file that may still being written because the
file is so large (7-10MB).
I get an error:

The process cannot access the file 'c:\TestDocs\XMLFiles\492172.XML' because
it is being used by another process
This is when doing:

CheckFileBeingUsed(xmlFile); // below - should give me up to 15
seconds to finish writing the file
fs = new FileStream(xmlFile, FileMode.Open, System.IO.FileAccess.Read);

I tried the following routine which should get an error if still in use. It
should run for 15 seconds (5 * 3), but even though the file is being
accessed (which I know because the FileStream error happens after the call),
it jumps out after the first call and never does go to the catch.

Why is this?
**********************************************
public bool CheckFileBeingUsed(string fileName)
{
// Check to see if file is in use. If very large it may be.

bool inUse = false;
for (int i = 0; i < 5; i++)
{
try
{
System.IO.File.Open(fileName, FileMode.Open,
System.IO.FileAccess.Read, FileShare.None);
inUse = false;
break;
}
catch (System.IO.IOException exp)
{
inUse = true;
System.Threading.Thread.Sleep(3000); // Wait 3
seconds and try again
}

}
return inUse;
}
**********************************************

Thanks,

Tom
Jun 27 '08 #1
9 5381
On Tue, 10 Jun 2008 17:48:27 -0700, tshad <ts***@dslextreme.comwrote:
I am trying to get access to a file that may still being written because
the
file is so large (7-10MB).
I get an error:

The process cannot access the file 'c:\TestDocs\XMLFiles\492172.XML'
because
it is being used by another process
This is when doing:

CheckFileBeingUsed(xmlFile); // below - should give me up to 15
seconds to finish writing the file
I don't understand this comment. The method you posted will not return
until it's given up or has opened the file itself. Either way, how does
that "give me up to 15 seconds to finish writing the file"?

But, really...that doesn't matter. The important part is the rest of the
discussion:
fs = new FileStream(xmlFile, FileMode.Open,
System.IO.FileAccess.Read);

I tried the following routine which should get an error if still in
use. It
should run for 15 seconds (5 * 3), but even though the file is being
accessed (which I know because the FileStream error happens after the
call),
it jumps out after the first call and never does go to the catch.
The file is being accessed because you just opened it.

Hopefully, this helps illustrate the fallacy behind your approach. Don't
open a file just to check to see if you can open. Just try to open it to
_use_ it! In addition to the specific problem, the code you posted also
won't work because between the time you think you've determined whether
the file can be opened or not, the status of the file could change. Most
unhelpfully, you could find the file available, but then it could become
unavailable by the time you get to try to open it.

If you want to include some sort of retry logic, so that you can
gracefully deal with situations where something else might also be using
the file, then do so. But once you've successfully opened the file, just
use the opened file. Anything less is just pointless file-twiddling.

Pete
Jun 27 '08 #2
Peter Duniho wrote:
On Tue, 10 Jun 2008 17:48:27 -0700, tshad <ts***@dslextreme.com>
wrote:
>I am trying to get access to a file that may still being written
because the
file is so large (7-10MB).
I get an error:

The process cannot access the file 'c:\TestDocs\XMLFiles\492172.XML'
because
it is being used by another process
This is when doing:

CheckFileBeingUsed(xmlFile); // below - should give me up to
15 seconds to finish writing the file

I don't understand this comment. The method you posted will not
return until it's given up or has opened the file itself. Either
way, how does that "give me up to 15 seconds to finish writing the
file"?
What is happening here is I am going to process this xml file that has lots
of images in it and is very large.

I am getting it too quick and I need to make the file has been completely
written and closed (so I don't get the file being accessed message) before I
start processing it.

The 15 seconds (probably not enough in some cases) is the 3 second delay for
5 loops. If I can access it right away, there would be no delay.

The problem is that the file is being written to and I am trying to open it
exclusively (FileShare.none). If someone already has it open (only when
being put in the folder), the this should give me an error and go to the
catch area. I assume that is how this is supposed to work (got the code
elsewhere).

But as you say later, I should just put the loop and try/catch around the
FileStream call, then when it becomes available I will already have it.
>
But, really...that doesn't matter. The important part is the rest of
the discussion:
> fs = new FileStream(xmlFile, FileMode.Open,
System.IO.FileAccess.Read);

I tried the following routine which should get an error if still in
use. It
should run for 15 seconds (5 * 3), but even though the file is being
accessed (which I know because the FileStream error happens after the
call),
it jumps out after the first call and never does go to the catch.

The file is being accessed because you just opened it.

Hopefully, this helps illustrate the fallacy behind your approach. Don't
open a file just to check to see if you can open. Just try to
open it to _use_ it! In addition to the specific problem, the code
you posted also won't work because between the time you think you've
determined whether the file can be opened or not, the status of the
file could change. Most unhelpfully, you could find the file
available, but then it could become unavailable by the time you get
to try to open it.
In my case, this wouldn't happen. I am watching the folder with a
filewatcher Service and the only access would be the person dropping the
file in my folder and my accessing it.
>
If you want to include some sort of retry logic, so that you can
gracefully deal with situations where something else might also be
using the file, then do so. But once you've successfully opened the
file, just use the opened file. Anything less is just pointless
file-twiddling.
Probably right.

I am still curious as to why the FileShare.none wouldn't create an error, if
the file were still being written to.

Thanks,

Tom
>
Pete

Jun 27 '08 #3
On Tue, 10 Jun 2008 18:38:46 -0700, tshad <ts***@dslextreme.comwrote:
>[...]
Most unhelpfully, you could find the file
available, but then it could become unavailable by the time you get
to try to open it.

In my case, this wouldn't happen.
Says you.

Unless you have somehow set the file permissions so that only your process
has access to the file, you cannot assume in your code that the file will
remain unavailable just because at some point in time it was.
I am watching the folder with a
filewatcher Service and the only access would be the person dropping the
file in my folder and my accessing it.
That may be the typical case, even by far. But you can't assume it.
>If you want to include some sort of retry logic, so that you can
gracefully deal with situations where something else might also be
using the file, then do so. But once you've successfully opened the
file, just use the opened file. Anything less is just pointless
file-twiddling.

Probably right.

I am still curious as to why the FileShare.none wouldn't create an
error, if
the file were still being written to.
It probably doesn't. The most likely reason that the file is inaccessible
is because _your_ process has it open, due to the successful call to
File.Open(). That's what I said before, and I still believe it. :)

Pete
Jun 27 '08 #4
tshad wrote:
....
>
I am getting it too quick and I need to make the file has been completely
written and closed (so I don't get the file being accessed message) before I
start processing it.
The simple solution to ensure that the file is complete before it is
used is to store it with a temporary name and rename it to the final
filename when finished.
String tmp_filename = filename + ".tmp";
....
if (File.Exists(filename))
File.Delete(filename);
File.Move(tmp_filename, filename);
--
Bjørn Brox
Jun 27 '08 #5
I've seen this come up so many times. Congratulations on the first
common-sense, simple and logical solution I believe I've seen.
--Peter
"Bjørn Brox" <bp****@gmail.comwrote in message
news:48********@news.broadpark.no...
tshad wrote:
...
>>
I am getting it too quick and I need to make the file has been completely
written and closed (so I don't get the file being accessed message)
before I start processing it.
The simple solution to ensure that the file is complete before it is used
is to store it with a temporary name and rename it to the final filename
when finished.
String tmp_filename = filename + ".tmp";
...
if (File.Exists(filename))
File.Delete(filename);
File.Move(tmp_filename, filename);
--
Bjørn Brox
Jun 27 '08 #6
On Thu, 12 Jun 2008 17:13:23 -0700, Peter Bromberg [C# MVP]
<pb*******@nospamDood.yahoo.comwrote:
I've seen this come up so many times. Congratulations on the first
common-sense, simple and logical solution I believe I've seen.
Huh. Actually, Bjørn's proposal is reasonably common. But I had the
impression from the OP that he doesn't have the luxury of picking the
filename for whatever output's the file.

That said, yes...I agree. If he _does_, the Bjørn's suggestion works the
best. :)

Pete
Jun 27 '08 #7

"Bjørn Brox" <bp****@gmail.comwrote in message
news:48********@news.broadpark.no...
tshad wrote:
...
>>
I am getting it too quick and I need to make the file has been completely
written and closed (so I don't get the file being accessed message)
before I start processing it.
The simple solution to ensure that the file is complete before it is used
is to store it with a temporary name and rename it to the final filename
when finished.
String tmp_filename = filename + ".tmp";
...
if (File.Exists(filename))
File.Delete(filename);
File.Move(tmp_filename, filename);
Not sure how this would work.

Does it stop at the File.Exists until it is finished copying?

Thanks,

Tom
>
--
Bjørn Brox

Jun 27 '08 #8

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 12 Jun 2008 17:13:23 -0700, Peter Bromberg [C# MVP]
<pb*******@nospamDood.yahoo.comwrote:
>I've seen this come up so many times. Congratulations on the first
common-sense, simple and logical solution I believe I've seen.

Huh. Actually, Bjørn's proposal is reasonably common. But I had the
impression from the OP that he doesn't have the luxury of picking the
filename for whatever output's the file.
Correct.

I am not picking the file name.

The user is putting a file into a folder I am watching with my Windows
Service program. It can be anything.

As soon as my FileWatcher program sees the file, it starts to process it.

Thanks,

Tom
That said, yes...I agree. If he _does_, the Bjørn's suggestion works the
best. :)

Pete

Jun 27 '08 #9
On Sat, 14 Jun 2008 22:51:01 -0700, tshad <tf*@dslextreme.comwrote:
I am not picking the file name.

The user is putting a file into a folder I am watching with my Windows
Service program. It can be anything.

As soon as my FileWatcher program sees the file, it starts to process it.
Well, I might not have been direct enough in previous posts. I can't tell
from your follow-up posts whether your question has been answered
satisfactorily or not.

The only right way to do this is to repeatedly attempt to open the file
with exclusive access unless you succeed. But don't do this the way
you've posted. Just open the file as you want to use it, and then if and
when you succeed, use it.

Depending on what latency is acceptable to you, you should probably put a
delay in between each attempt. Half a second to a second might be
reasonable. How you delay is up to you; I would use a timer, but if you
just want to let a thread sit and try over and over, with a call to
Thread.Sleep() in between, that'd be fine too.

Is there any other information you need?

Pete
Jun 27 '08 #10

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

Similar topics

5
by: Laphan | last post by:
Hi All I've been looking at having my Javascript file as an ASP one to try and stop general prying eyes, eg: <SCRIPT LANGUAGE="javascript" SRC="js-something.asp"> </SCRIPT> and this seems...
4
by: zzapper | last post by:
Hi, I've inherited a mysql database with many apparently redundant tables (probably abandoned projects). Without analysing the webpages many of which are also redundant; is there any MYSQL query...
3
by: John Bailo | last post by:
With OS400, all files are objects that can be accessed relationally. When I view the file system from Navigator (windows client), I see there is the Database and SQL tables, then there is the...
3
by: happy | last post by:
/* Book name : The prodessional programmers guide to C File name : E:\programs\tc\iti01\ch09\main\01setupm.c Program discription: file setuping -up -Version 01-ver01-W Logic ...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
8
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
1
by: Christian Heimes | last post by:
William Purcell wrote: (33188, 362259, 2053L, 1, 0, 0, 1690, 1218550501, 1218118498, 1218118498) 1218118498.0
0
by: Virgil Stokes | last post by:
Christian Heimes wrote: Is it possible to change the time stamp of a file (Win2K platform)? If yes, how?
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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

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.