471,317 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Is there a standard way to check whether or not a file is in use?

Try to do something with it, and look at the exception? Some other
way?

Thanks!
Apr 11 '08 #1
3 1354
On Fri, 11 Apr 2008 10:48:49 -0700, sherifffruitfly
<sh*************@gmail.comwrote:
Try to do something with it, and look at the exception? Some other
way?
It depends on why you want to know. If you're trying to find out whether
it's in use because you yourself want to use it, then yes...the best,
"standard" way is to simply attempt the operation and see if that
operation succeeds.

If you want to simply inspect the state without affecting the file, I
don't think that's possible from within .NET. However, there are
lower-level OS mechanisms that you could use. This isn't an appropriate
forum for learning about them though. You'd be better off in a newsgroup
specific to unmanaged Win32 programming.

Pete
Apr 11 '08 #2
On Apr 11, 11:04 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 11 Apr 2008 10:48:49 -0700, sherifffruitfly

<sherifffruit...@gmail.comwrote:
Try to do something with it, and look at the exception? Some other
way?

It depends on why you want to know. If you're trying to find out whether
it's in use because you yourself want to use it, then yes...the best,
"standard" way is to simply attempt the operation and see if that
operation succeeds.
Good enough - I just typically shy away from "exception-based
programming", so I thought I'd see if there were another common way.
Thanks a bunch!
Apr 11 '08 #3
On Fri, 11 Apr 2008 11:16:49 -0700, sherifffruitfly
<sh*************@gmail.comwrote:
Good enough - I just typically shy away from "exception-based
programming", so I thought I'd see if there were another common way.
Not that I know of. Exceptions are expensive, granted. But so is file
i/o. :)

In any case, logically if you are going to need access to the file anyway,
it doesn't do you any good to check the file's status beforehand, since
the status could change between the time you check and the time you try to
use the file.

Even if the file's accessible, it might not be by the time you try to use
it. And even in the situation where checking first could theoretically
save you the cost of the exception, now you've got the potential for a
false negative; that is, the check could fail even though by the time you
try to use the file, you could have succeeded.

Pete
Apr 11 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Marko Faldix | last post: by
2 posts views Thread by bruce varley | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
31 posts views Thread by Bill Cunningham | last post: by
7 posts views Thread by andylcx | last post: by
270 posts views Thread by jacob navia | last post: by
126 posts views Thread by Dann Corbit | last post: by

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.