Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I work in an Engineering and Design group that uses CAD. Occasionally, we get hanging file locks that require us to remove the lock. Due to security constraints, we do not have access to the server on which the locked file resides.

I created an admin application that connects to a shared server and, using impersonation, gets the list of all open files for non-admin users. It does not rely on the openfiles.exe program to do so, it builds the list using the following code:

C#
DirectoryEntry sessQuery = new DirectoryEntry("WinNT://" + serverName + "." + sWhere + ".apci.com/LanmanServer",
    sWhere + "\\" + sWho,
    sAuth,
    AuthenticationTypes.ServerBind);
IADsFileServiceOperations fso = sessQuery.NativeObject as IADsFileServiceOperations;
IADsCollection resources = fso.Resources();
foreach (IADsResource res in resources)
{
    try
    {
        dT.Rows.Add(res.Name, res.Path, res.User);
    }
    catch
    {
        // Do Nothing - just skip
    }
}


This gives me a data table (defined above the code snippet) that contains the name (which is, in reality, the file ID), full path, and user that has the file open.

When using Shared Files -> Open Files on the server management console, there is a column called Open Mode that identifies whether the file is open read-only or write-read. This is useful information for our admin team, used to determine if the file is being referenced or actively edited.

The problem is that I can't figure out where this information is coming from. I haven't found anything in the IADsResource or related interfaces (from IADsFileServiceOperations) that might provide that information.

Where can I get that information for the currently open files?

Thanks in advance,
Gary
Posted
Updated 3-Jul-14 6:34am
v2
Comments
Prasad Avunoori 3-Jul-14 23:40pm    
Before accessing/Edit the file you want to know whether the file is open or not, right?
Gary E. Shay, Jr. 14-Jul-14 11:29am    
Prasad, not quite. I want to know all the open files on the server so I can close the one that's causing a file lock. The list I get indicates that the shared file is open - I want to know how it's opened - read or read/write.

1 solution

you can use LockCount property of resources i.e. res.LockCount

add res.lockCount to your table. if the LockCount > 0 then then it is readonly files.

try
{
dT.Rows.Add(res.Name, res.Path, res.User ,res.LockCount);
}
catch
 
Share this answer
 
Comments
Gary E. Shay, Jr. 14-Jul-14 11:32am    
I've tried this and res.LockCount is 0 for all files. I'm still verifying that I'm not interpreting it incorrectly, but this doesn't seem to give me what I'm trying to get.
Gary E. Shay, Jr. 15-Jul-14 7:25am    
I've confirmed that it does give you lock information, but only when the file is currently, ACTIVELY being updated. For example, if you're in a word document and have changes pending, it is not actively locked. However, an access database file will nearly always appear locked when updating. Still searching...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900