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

How to check if a file is locked?

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
5 20241
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
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
" 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Pekka Niiranen | last post by:
Hi, I have used the following example from win32 extensions: -----SCRIPT STARTS---- import win32file import win32con import win32security import pywintypes
2
by: Ken Durden | last post by:
I'm setting up an interface where clients must perform external locking before calling certain commands. I do this to force them to specify the duration the action they performed must persist...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: Peter | last post by:
Question one: How do you detect if a file is locked. I have a program that is cleaning up a temp directory and I want to scan the files to see if they are locked before I try to delete them. ...
4
by: Xero | last post by:
hello. how do you check if a file (say, a txt one) is being used by another process? thanks. -- Xero http://www.chezjeff.net My personal web portal
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
6
by: elake | last post by:
I found this thread about a pst file in Windows being locked and I am having the same issue. ...
1
by: Joe | last post by:
any other way than Try / Catch / Finally ??
7
by: xerc | last post by:
Hey all, So I have a script that runs pretty much every 5 minutes or so to look for updated files locally on a server. New files (ranging in size from 1KB - 2MB) are then FTP'd to another server....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.