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

  • Thread starter Thread starter sherifffruitfly
  • Start date Start date
S

sherifffruitfly

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

Thanks!
 
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!
 
sherifffruitfly said:
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!

Considering that a file is a volatile object, I'm not sure you can avoid
using exceptions in any case, depending on what specifically you want to
do of course.

If all you want to do is to check the status of a file before attempting
to do something, then the state of that file might have changed from
your check to the point where you attempted your operation.

And besides, there's nothing wrong with using exceptions. As with all
things, there are areas where there are better constructs (depending on
the situation), but you should not strive to avoid something just
because of a feeling.

One of the places where I'd look at avoiding exceptions would be in a
very high volume transaction processing service, where perhaps the cost
of doing a few more if-statements would be far less than the cost of an
exception.

However, in the world occupied by a file system and a physical disk, I
don't think you should have to worry too much about the performance of
an occasional exception.
 

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

Back
Top