473,402 Members | 2,046 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,402 software developers and data experts.

Open files and locks

Is there a way in the dotnet to determine what files have locks and then
what process is locking the file?
Nov 15 '05 #1
20 2510
I'm afraid you'll have to p/invoke ntdll.dll to get what you want.

Yves

"Mark" <no************@technologist.com> schreef in bericht
news:%2****************@TK2MSFTNGP12.phx.gbl...
Is there a way in the dotnet to determine what files have locks and then
what process is locking the file?

Nov 15 '05 #2

Hi Mark,

Thank you for posting in the community!

Based on my understanding, you want to determine if one file is locked by
another process, and which process locked this file.

=============================================
For your first part concern, you can use exception to get this done:
Use you .Net application to access this file, if this file is locked, then
an exception will generate, in the exception handler, you can determine
that this file is locked by another process.

For your second part concern, .Net did not expose this function for users.
Actually, event in win32 environment, there are not a documented way to get
this information.
You may use some undocumented Windows 2000/NT native API to get this done.
These APIs are included in ntdll.dll.
In .Net, you can use P/invoke to use them.

For more information, please refer to:
http://groups.google.com/groups?hl=e...ame=right&th=d
392ffdb818c5665&seekm=vL7bp2axCHA.3256%40cpmsftngx a06#link1

Also, the below article "Inside the Native API" may give you more
information on native api:
http://www.sysinternals.com/ntw2k/info/ntdll.shtml

There is a "Process Explorer" tool in sysinternals website, it may help
you, please refer to:
http://www.sysinternals.com/ntw2k/fr.../procexp.shtml

===========================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #3

Hi Mark,

Does my reply make sense to you?

If you still have any concern, please feel free to feedback, I will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4
Thank you for the response. First, if a file has a lock and throws an
exception that tells me its locked but not who is locking it. It seems
that I will need to look into pinvoke.

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:Ht**************@cpmsftngxa07.phx.gbl...

Hi Mark,

Does my reply make sense to you?

If you still have any concern, please feel free to feedback, I will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5

"Mark" <no************@technologist.com> wrote in message
news:u4**************@TK2MSFTNGP10.phx.gbl...
Thank you for the response. First, if a file has a lock and throws an
exception that tells me its locked but not who is locking it. It seems
that I will need to look into pinvoke.

Not necessarily, you can use System.Management and WMI to query the
Processes associated with a file. Here's a sample to get you started.

Willy.

using System;
using System.Management;
// File - process relation query
class App {
public static void Main() {
ShowHDproperties(@"select * from cim_datafile where name=
'c:\\windows\\system32\\wsock32.dll'");
}

static void ShowHDproperties(string objectQry) {

SelectQuery query = new SelectQuery(objectQry);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
PropertyDataCollection oProperties = mo.Properties;
foreach (PropertyData prop in oProperties ) {
if(hdProperty.Value !=null) // Show only non-null property values
Console.WriteLine("Property = {0}\t Value = {1}",
prop.Name, prop.Value);
}
// Get related processes
foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process")) {
Console.WriteLine("--------------- File opened by
(Processes) -----------------");
ShowHDassocProperties(b.ToString());
b.Dispose();
}

}
}
static void ShowHDassocProperties(string objectClass) {
ManagementObject hd;
using(hd = new ManagementObject (objectClass))
{
hd.Get();
PropertyDataCollection hdProperties = hd.Properties;
Console.WriteLine("Commandline: {0} ProcessID: {1}"
,hdProperties["CommandLine"].Value, hdProperties["ProcessID"].Value);

}
}
}

Nov 15 '05 #6
Ignore the sample in my previous reply, here's a complete and working
sample.

using System;
using System.Management;
// Search the processes which have a certain file open
class App {
public static void Main() {
WhoHasThisFileOpen(@"c:\\windows\\system32\\wsock3 2.dll");
}
static void WhoHasThisFileOpen(string objectQry) {
SelectQuery query = new SelectQuery("select name from cim_datafile where
name ='" + objectQry +"'" );
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query)) {
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine("File Name: {0} \nIs currently opened by:",
mo.Properties["Name"].Value);
// Get processes having this File open
foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process")) {
ShowProcessProperties(b.ToString());
b.Dispose();
}
}
}
}
static void ShowProcessProperties(string objectClass) {
using(ManagementObject process = new ManagementObject (objectClass))
{
process.Get();
PropertyDataCollection processProperties = process.Properties;
Console.WriteLine("ProcessID: {0,6} \tCommandLine: {1}" ,
processProperties["ProcessID"].Value,
processProperties["CommandLine"].Value);
}
}
}

Willy.
Nov 15 '05 #7
I tried this on my Windows XP machine with VS 2003 Ent and it does not find
anything!

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
Ignore the sample in my previous reply, here's a complete and working
sample.

using System;
using System.Management;
// Search the processes which have a certain file open
class App {
public static void Main() {
WhoHasThisFileOpen(@"c:\\windows\\system32\\wsock3 2.dll");
}
static void WhoHasThisFileOpen(string objectQry) {
SelectQuery query = new SelectQuery("select name from cim_datafile where
name ='" + objectQry +"'" );
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query)) {
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine("File Name: {0} \nIs currently opened by:",
mo.Properties["Name"].Value);
// Get processes having this File open
foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process")) {
ShowProcessProperties(b.ToString());
b.Dispose();
}
}
}
}
static void ShowProcessProperties(string objectClass) {
using(ManagementObject process = new ManagementObject (objectClass))
{
process.Get();
PropertyDataCollection processProperties = process.Properties;
Console.WriteLine("ProcessID: {0,6} \tCommandLine: {1}" ,
processProperties["ProcessID"].Value,
processProperties["CommandLine"].Value);
}
}
}

Willy.

Nov 15 '05 #8
What file path did you use?

Willy.
"Mark" <no************@technologist.com> wrote in message
news:eA*************@tk2msftngp13.phx.gbl...
I tried this on my Windows XP machine with VS 2003 Ent and it does not find
anything!

Nov 15 '05 #9

Hi Mark,

Yes, I have tried Willy's sample code, it should work well.

I also use WinXP and Visual Studio.Net 2003 Architecture Enterprise
version.

In Willy's sample code, he use wsock32.dll as the parameter. I think your
wsock32.dll may not in the path "c:\\windows\\system32", you may check it
is a correctly path.

Also, you can debug into the code to see where is the problem.

Anyway, if you have further concern, please feel free to feedback.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #10
Jeffey,
In general this works as expected, of course there are constraints like the
callers token privileges, and the DACL put on the File objects, it's also
possible that a file is attached to a kernel subsystem, in which case this
won't work.

Willy.

"""Jeffrey Tan[MSFT]""" <v-*****@online.microsoft.com> wrote in message
news:iv**************@cpmsftngxa07.phx.gbl...

Hi Mark,

Yes, I have tried Willy's sample code, it should work well.

I also use WinXP and Visual Studio.Net 2003 Architecture Enterprise
version.

In Willy's sample code, he use wsock32.dll as the parameter. I think your
wsock32.dll may not in the path "c:\\windows\\system32", you may check it
is a correctly path.

Also, you can debug into the code to see where is the problem.

Anyway, if you have further concern, please feel free to feedback.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #11

Hi Willy,

Yes, there can be many possibilities. Your information is very useful for
the community.

Thanks for your contribute!

Anyway, let's wait the feedback from Mark.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #12
I was on vacation so I will check it again this week. It seems to me that
there must be some open file event I can hook into to see if a file is
opened similar to a virus scanner.

"""Jeffrey Tan[MSFT]""" <v-*****@online.microsoft.com> wrote in message
news:n3**************@cpmsftngxa07.phx.gbl...

Hi Willy,

Yes, there can be many possibilities. Your information is very useful for
the community.

Thanks for your contribute!

Anyway, let's wait the feedback from Mark.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #13

Hi Mark,

Thanks very much for your feedback.

.Net does not expose this open file event feature. Because .Net
FileSystemWatcher class actually encapsulate the Win32 API
ReadDirectoryChangesW(), there is a 1:1 relationship between them. So the
FileSystemWatcher class can only provide the function that
ReadDirectoryChangesW() expose. If you look into the ReadDirectoryChangesW
function, you will see that it does not expose the function to monitor the
open file notification. So FileSystemWatcher does not expose this event
also.

Anyway, I will wait for your further feedback of the WMI solution. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #14
I got the sample to work. It was looking for the operating system in
c:\windows when it was in c:\winnt.

The sample makes the assumption that I know what files I want to check. I
don't. I need to find the open files. You mentioned earlier that I could
use pinvoke on the ntdll.dll. Where would I find was functions to import
that are in ntdll.dll.

"""Jeffrey Tan[MSFT]""" <v-*****@online.microsoft.com> wrote in message
news:Oi***************@cpmsftngxa06.phx.gbl...

Hi Mark,

Thanks very much for your feedback.

Net does not expose this open file event feature. Because .Net
FileSystemWatcher class actually encapsulate the Win32 API
ReadDirectoryChangesW(), there is a 1:1 relationship between them. So the
FileSystemWatcher class can only provide the function that
ReadDirectoryChangesW() expose. If you look into the ReadDirectoryChangesW
function, you will see that it does not expose the function to monitor the
open file notification. So FileSystemWatcher does not expose this event
also.

Anyway, I will wait for your further feedback of the WMI solution. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #15

Hi Mark,

Sorry for letting you wait for so long time.

Just as I stated in original reply, the functions in ntdll.dll are not
public documented, so I can not provide you the official document. But in
that reply, I have provided you some links which documented some of these
APIs in a non-official way. You may use it for your information.

I think the WMI way is a more suitable way of getting this done. As I see
that your concern is you want to know which files are locked. Actually, I
think the more realistic thing you want to do is getting all the files in a
certain directory that are opened. (Because all the files are opened in the
system is too large an amount)
To get this done, I think you may loop through all the files in that
directory, then for each file, use WMI to query the locked processes.

Does this meet your need? If you still have any concern, please feel free
to feedback.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #16

Hi Mark,

Addtionally, to loop through the directory in .Net, please refer to
DirectoryInfo class in MSDN

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #17

Hi Mark,

Does my reply make sense to you ?

If you still have anything unclear, please feel free to tell me.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #18
Its clear and I can find out if the file is locked but not who locked it.

"""Jeffrey Tan[MSFT]""" <v-*****@online.microsoft.com> wrote in message
news:mZ****************@cpmsftngxa06.phx.gbl...

Hi Mark,

Does my reply make sense to you ?

If you still have anything unclear, please feel free to tell me.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #19
Hi Mark,

Sorry, I am not quite understand your meanning.

As you said, you can find out if the file is locked. So I think you can use
Willy's solution through WMI to get which processes locked this file.

Why did you say "but not who locked it."? Do you still haven concern ?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #20
Mark,

What exactly do you mean with 'who locked it''?

Willy.

"Mark" <no************@technologist.com> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
Its clear and I can find out if the file is locked but not who locked it.

"""Jeffrey Tan[MSFT]""" <v-*****@online.microsoft.com> wrote in message
news:mZ****************@cpmsftngxa06.phx.gbl...

Hi Mark,

Does my reply make sense to you ?

If you still have anything unclear, please feel free to tell me.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.


Nov 15 '05 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mark | last post by:
Hello; Here is what I wish to do: Click on a PDF link and have it open as a full screen window - not as a predetermined size. Sounds simple? I want to run the command from within the...
8
by: pakku | last post by:
We are being faced with a situation where our Windows Server based UDB (V7) crashes every so often due to log files getting too full. We are seeing lots of messages in the logs about full log files...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
3
by: Earl Teigrob | last post by:
I want to create a web based file manager for my web site that allows me to move/copy/rename/delete files and directories in my web site. Just from my experience with FTP software, this can be a...
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...
7
by: s w dunn | last post by:
Hello, I am in charge of maintaining a little ACCESS database used by our purchasing department. I need to add some new values to a table, but I can't open the .be database in order to access the...
1
by: shenanwei | last post by:
I have db2 v8.2.5 on AIX V5.3 with all the switches on Buffer pool (DFT_MON_BUFPOOL) = ON Lock (DFT_MON_LOCK) = ON Sort ...
2
by: Ryan Liu | last post by:
Hi, I have few db write and read to execute, so I use transaction. Is that a problem or is that a regular way that I only use transaction on some cmds only, and other cmds I do not use...
1
by: barry.zhao | last post by:
Hi, I have a python program that constantly updates an excel spreadsheet. I would like to be able to view its updates while using excel to edit other excel files. Below are the test codes I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.