By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

How to check if a file is locked?

P: n/a
The only examples I have seen on how to check if a file is locked is to
try to open it a catch an exception.

MS has stated that you should never use error trapping in this manner.

Is there no other way to identify if a file is locked w/o trapping an
exception?

Jul 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
cmay,

This isn't so much as an answer, as it is a point in the right
direction.

You should be able to access the MS Computer Management Counsole
snap-in in the MMC programmatically, if you have the right credentials.

Try this as a starting point:

MMC Programmer's Guide
http://msdn.microsoft.com/library/de...enu_object.asp

MMC Reference:
http://msdn.microsoft.com/library/de...nd_methods.asp

A link to some one who tried this:
http://groups.google.com/group/micro...fa95d02201adcb

cmay wrote:
The only examples I have seen on how to check if a file is locked is to
try to open it a catch an exception.

MS has stated that you should never use error trapping in this manner.

Is there no other way to identify if a file is locked w/o trapping an
exception?
Jul 6 '06 #2

P: n/a
I am not sure of the relevance of the first post on here perhaps it went to
the wrong thread?

For file locking you would have to handle this exception anyways. The file
may not be locked when you check it but could then be locked when you try to
go to open it. The checking would however remove many instances of ther
exception. That said in this case your best best is to trap the exception as
there is no other good way of doing this, you could also use pinvoke
(CreateFile) to avoid having the exception thrown but the exception is
really not that expensive. You should then wait a little bit before trying
again if thats your overall goal.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"cmay" <cm**@walshgroup.comwrote in message
news:11**********************@s16g2000cws.googlegr oups.com...
The only examples I have seen on how to check if a file is locked is to
try to open it a catch an exception.

MS has stated that you should never use error trapping in this manner.

Is there no other way to identify if a file is locked w/o trapping an
exception?

Jul 7 '06 #3

P: n/a
" The file may not be locked when you check it but could then be locked
when you try to
go to open it. "

Good point.

Thanks for the input!
Chris

Jul 7 '06 #4

P: n/a
Greg Young wrote:
For file locking you would have to handle this exception anyways. The file
may not be locked when you check it but could then be locked when you try to
go to open it. The checking would however remove many instances of ther
exception. That said in this case your best best is to trap the exception as
there is no other good way of doing this, you could also use pinvoke
Just out of curiosity, do you know how other programs can determine
which processes have which files open? For example, Process Explorer
at sysinternals.com can show the processes and which files they have
open. How is this accomplished?

Jul 10 '06 #5

P: n/a
Greg,

Sorry, I didn't mean to confuse the issue. My solution, using Dot Net,
would be first determine if the files is open (trap the error). But
what has to happen after that to close the file?

Using the MMC programatically, you could transverse the Computer
Management Counsole / Open Files, looking for all occurances of the
file and taking appropriate action to close the file.

For example, we have a process that does server file backup' s nightly.
Some of the files are .MDB's and .LDB's. Every one has gone home for
the day and they are aware of nightly maintenance. So, as one of the
"manual" steps the operator has to do before they leave is delete the
LDB's and MDB's from the Open Files list. In a seanse, they are
closing these server files before our maintnance starts.

This is where I saying the manual steps could be automated by
programatically transversing the Open Files list of the Computer
Management Counsole (v2.0) using the MMC.
Chris Dunaway wrote:
Greg Young wrote:
For file locking you would have to handle this exception anyways. The file
may not be locked when you check it but could then be locked when you try to
go to open it. The checking would however remove many instances of ther
exception. That said in this case your best best is to trap the exception as
there is no other good way of doing this, you could also use pinvoke

Just out of curiosity, do you know how other programs can determine
which processes have which files open? For example, Process Explorer
at sysinternals.com can show the processes and which files they have
open. How is this accomplished?
Jul 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.