How to check if a file is locked?

  • Thread starter Thread starter cmay
  • Start date Start date
C

cmay

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?
 
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/default.asp?url=/library/en-us/mmc/mmc/contextmenu_object.asp

MMC Reference:
http://msdn.microsoft.com/library/d.../en-us/mmc/mmc/mmc_interfaces_and_methods.asp

A link to some one who tried this:
http://groups.google.com/group/micr...+2.0+programmatically&rnum=1#a4fa95d02201adcb
 
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
 
" 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
 
Greg said:
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?
 
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.
 
Back
Top