473,386 Members | 1,652 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,386 software developers and data experts.

How to enumerate open files using DirectoryServices

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
6 13293
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

"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
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
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

"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
Willy,

Thank you for the help on getting me a solution

Rich

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

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

Similar topics

5
by: Jeff Grundy | last post by:
How do I enumerate the machines domain, then enumerate the shares of a machine?
1
by: Corne Grotius | last post by:
Hiya, I'm trying to create a new site on IIS 6.0 using ADSI and C# using the following code: DirectoryEntry W3SVC = new DirectoryEntry("IIS://" + ServerName + "/w3svc", Username, Password,...
8
by: Sameh Ahmed | last post by:
Hello there I need to get a list of domain controllers to a domain I bind too using it's name rather than the DN. I am trying a query looking for objectClass=nTDSDSA but this returns 0 results I...
23
by: BH Jodo Kast | last post by:
Hi, I found this handy script and I'm trying to convert it to VB.NET. It pops up a list of members in the Administrators/Builtin group. Can't seem to get DirectorySearcher or DirectoryEntry...
7
by: turbon | last post by:
Hello, I am writing code, which will copy webServices from one IIS 6.0 webserver to another and using DirentoryServices to achieve this purpose. And I have problems with authentication - I get an...
2
by: Jay | last post by:
Hi, This is Jay Mehta. I have this problem when using LDAP. I extract names and EmailId's of all those present from LDAP and populate in a datagrid. Now when run locally, it is running...
6
by: Mark Rae | last post by:
Hi, I'm in the process of updating an ASP.NET v1.1 web app to v2. The app uses ActiveDirectory a great deal, and I'm trying to use the new System.Collections.Generic namespace where possible,...
8
by: eight02645999 | last post by:
hi say i want to enumerate lines of a file eg for n,l in enumerate(open("file")): # print next line ie is there a way to print out the next line from current line using the above?. Or do i...
2
by: DoB | last post by:
Hi, I would like to get the list of full paths of all files that are currently opened by any process in the system. Is this possible? In short: my program moves a bunch of directories and I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.