Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Retrieving list of file locks?

33 views
Skip to first unread message

Jonas Nordlund

unread,
Sep 14, 2002, 11:21:43 AM9/14/02
to
Is there a class in the .NET Framework to deal with file
lock information? Like what remote user is locking a
file? And which process on the local computer is locking
(has a handle to) a file?

The reason I'm asking is because sometimes bugged (?)
applications fail to release their lock and the result is
a file you can (usually) only read from.

Or do I have to dig into the Win32 API's to do this? I
saw something about NetFileGetInfo but I was hoping for
a "cleaner" and easier way to do it... :-/

Willy Denoyette [MVP]

unread,
Sep 15, 2002, 12:40:49 PM9/15/02
to
The closest you can get is using DirectoryServices classes and ADSI.
Try following example.

using System;
using System.DirectoryServices;
using activeds; // import activeds.tlb using tlbimp.exe
// List all File shares on a system only for W2K, XP and higher
class Tester {
public static void Main() {
// Bind using explict credentials
using(DirectoryEntry container = new DirectoryEntry("WinNT://<serverName>/LanmanServer", "domain\\someadmin", "adminpwd",
AuthenticationTypes.ServerBind))
{
foreach(DirectoryEntry share in container.Children)
{
PropertyCollection pcoll = share.Properties;
try
{
foreach(string sc in pcoll.PropertyNames) {
Console.WriteLine(sc + ":\t" + (pcoll[sc])[0].ToString());
// Show Session and Resource info, when user count > 0
if (sc == "CurrentUserCount" && (int)pcoll["CurrentUserCount"][0] > 0)
{
IADsFileServiceOperations fso = container.NativeObject as IADsFileServiceOperations;
Console.WriteLine("----- Session info -------");
foreach(IADsSession session in fso.Sessions())
{
Console.WriteLine("\tComputer: {0}", session.Computer);
Console.WriteLine("\tUser: {0}", session.User);
Console.WriteLine("\tConnect time: {0}", session.ConnectTime);
Console.WriteLine("\tIdle time: {0}", session.IdleTime);
Console.WriteLine("\tName: {0}", session.Name);
}
IADsCollection resources = fso.Resources() as IADsCollection;
Console.WriteLine("----- Resource info -------");
try {
foreach(IADsResource resource in resources)
{
Console.WriteLine("\tPath: {0}", resource.Path);
Console.WriteLine("\tUser: {0}", resource.User);
Console.WriteLine("\tLockCount: {0}", resource.LockCount);
Console.WriteLine("\tName: {0}", resource.Name);
Console.WriteLine("------------------------------");
}
}
catch (System.Runtime.InteropServices.COMException ex){
// Watch Non-Fileshare resources like named pipes, these are not stored in the ADSI cache
Console.WriteLine(ex.Message);
}
}
}
Console.WriteLine("---------------------------------");
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}

Willy.

"Jonas Nordlund" <juga...@despammed.com> wrote in message news:1114301c25c02$6e781a50$37ef2ecf@TKMSFTNGXA13...

0 new messages