473,749 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 20279
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 programmaticall y, 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**@walshgrou p.comwrote in message
news:11******** **************@ s16g2000cws.goo glegroups.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.co m 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.co m 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
7326
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
18796
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 before any other threads can be allowed to perform some action. My object exposes a Mutex which is locked via a IDisposable-Sentry class. I want to throw an exception if certain functions are called without this Mutex already being locked, but I...
0
3939
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
0
1188
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. Question Two: If the file is locked and I have admin privilages how do you take ownership of the file and then delete it?
4
8714
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
19951
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 it, it throws an exception. How to solave this problem? So second process can wait until first process completes its processing on the file. Thanks in advance
6
6030
by: elake | last post by:
I found this thread about a pst file in Windows being locked and I am having the same issue. http://groups.google.com/group/comp.lang.python/browse_thread/thread/d3dee5550b6d3652/ed00977acf62484f?lnk=gst&q=%27copying+locked+files%27&rnum=1 The problem is that I have a script that can find the pst files on every machine in my network and back them up to a server for safe keeping. The problem is that when Outlook is running it locks the...
1
3239
by: Joe | last post by:
any other way than Try / Catch / Finally ??
7
2217
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. Without going into detail, one server is a content server, the other is a 'broadcast' server. Anywho, the problem seems to be with the FTP part. I'd say about 20 percent of the time, regardless of file size, something hangs up, such that the file...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.