How to enumerate open files using DirectoryServices

  • Thread starter Rich Crusco via DotNetMonster.com
  • Start date
R

Rich Crusco via DotNetMonster.com

I am looking for a way to convert the following vbs script to c#

Const ADS_SECURE_AUTHENTICATION = 1
strADMIN = "adminaccount"
strPASSWORD = "password"
strUSER = "useraccount"
Set dso = GetObject("WinNT:")
Set srv = DSO.OpenDSObject("WinNT://servername/LanmanServer", strADMIN,
strPASSWORD, ADS_SECURE_AUTHENTICATION)
Set colResources = srv.Resources
For Each objResource in colResources
If objResource.User = strUSER Then objResource.remove(objResource.Name)
Next


I have seen various c# code using DirectoryServices with the winnt:
provider to enumerate users, groups, shares, etc
But I cant seem to find out how to gather open file or session information.
I know that he below code would show me shares on a file server,
but how do I get it to show me open files and sessions ?

using System;
using System.DirectoryServices;

namespace NetAcademia.Samples
{
class App
{
static void Main()
{
DirectoryEntry root = new DirectoryEntry
("WinNT://servername/lanmanserver");

foreach(DirectoryEntry child in root.Children)
{
Console.WriteLine(child.Name);
}
}
}
}

Thanks for any insight or help with this problem
Rich
 
M

Manfred Braun

Hi,

I am not familar with .Net DirectoryServices [coming from scripting, feel
it's a nightmare]. In script, the analogy could possibly help you, it is as
follows:

Set objFSOps = GetObject("WinNT://<box>/lanmanserver")

For Each objSession In objFSOps.Sessions
WScript.Echo objSession.Name
Next

For Each objResource In objFSOps.Resources
WScript.Echo objResource.Name
Next

May be, that helps.

Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
W

Willy Denoyette [MVP]

Rich Crusco via DotNetMonster.com said:
I am looking for a way to convert the following vbs script to c#

Const ADS_SECURE_AUTHENTICATION = 1
strADMIN = "adminaccount"
strPASSWORD = "password"
strUSER = "useraccount"
Set dso = GetObject("WinNT:")
Set srv = DSO.OpenDSObject("WinNT://servername/LanmanServer", strADMIN,
strPASSWORD, ADS_SECURE_AUTHENTICATION)
Set colResources = srv.Resources
For Each objResource in colResources
If objResource.User = strUSER Then objResource.remove(objResource.Name)
Next


I have seen various c# code using DirectoryServices with the winnt:
provider to enumerate users, groups, shares, etc
But I cant seem to find out how to gather open file or session
information.
I know that he below code would show me shares on a file server,
but how do I get it to show me open files and sessions ?

using System;
using System.DirectoryServices;

namespace NetAcademia.Samples
{
class App
{
static void Main()
{
DirectoryEntry root = new DirectoryEntry
("WinNT://servername/lanmanserver");

foreach(DirectoryEntry child in root.Children)
{
Console.WriteLine(child.Name);
}
}
}
}

Thanks for any insight or help with this problem
Rich

Something like this should do the job...
Note that you need to add a reference to the activeds.tlb !!!!!

using (DirectoryEntry container = new
DirectoryEntry("WinNT://servername/LanmanServer")
{
IADsFileServiceOperations fso= container.NativeObject as
IADsFileServiceOperations;
if (fso != null)
{
foreach(IADsSession sess in fso.Sessions()) {
Console.WriteLine("Name : {0} \tUser: {1} \tComputer : {2}",
sess.Name, sess.User, sess.Computer);
}

IADsCollection resources = fso.Resources() as IADsCollection;
Console.WriteLine("----- Resource info -------");
foreach(IADsResource resource in resources)
{
try
{
Console.WriteLine("\tPath: {0}\tUser: {1}\tLockCount: {2}\tName:
{3}",
resource.Path, resource.User, resource.LockCount,
resource.Name);
}
catch (System.IO.FileNotFoundException ex){
// Watch Non-Fileshare resources like named pipes, these are not
stored in the ADSI cache
}
}
}
}

Willy.
 
R

Rich Crusco via DotNetMonster.com

Thank you Willy for the great solution

I would have used WMI but on of our file servers is a NAS device
which doesnt support WMI calls

Now all I need to do is make it remove the session and resources for a
given username,
I assume that Ill be able to call Remove() on the sessions and resources

Thanks
Rich
 
W

Willy Denoyette [MVP]

Rich Crusco via DotNetMonster.com said:
Thank you Willy for the great solution

I would have used WMI but on of our file servers is a NAS device
which doesnt support WMI calls

Now all I need to do is make it remove the session and resources for a
given username,
I assume that Ill be able to call Remove() on the sessions and resources

Ok to remove a session...

fso.Sessions().Remove(sess.Name);

Unfortunately, removing a Resource isn't implemented by the provider :-(.

Willy.
 
R

Rich Crusco via DotNetMonster.com

Willy,

Thank you for the help on getting me a solution

Rich
 
Top