By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

How to enumerate open files using DirectoryServices

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

"Rich Crusco via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote
in message news:0c******************************@DotNetMonste r.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

Nov 17 '05 #2

P: n/a

"Rich Crusco via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote
in message news:0c******************************@DotNetMonste r.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


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.
Nov 17 '05 #3

P: n/a
Hi Willy,

thanks a lot for the sample!!

Best regards,
Manfred

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:O$**************@TK2MSFTNGP15.phx.gbl...

"Rich Crusco via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote
in message news:0c******************************@DotNetMonste r.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


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.

Nov 17 '05 #4

P: n/a
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

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #5

P: n/a

"Rich Crusco via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:46******************************@DotNetMonste r.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


Ok to remove a session...

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

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

Willy.
Nov 17 '05 #6

P: n/a
Willy,

Thank you for the help on getting me a solution

Rich

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.