473,569 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to enumerate open files using DirectoryServic es

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

Const ADS_SECURE_AUTH ENTICATION = 1
strADMIN = "adminaccou nt"
strPASSWORD = "password"
strUSER = "useraccoun t"
Set dso = GetObject("WinN T:")
Set srv = DSO.OpenDSObjec t("WinNT://servername/LanmanServer", strADMIN,
strPASSWORD, ADS_SECURE_AUTH ENTICATION)
Set colResources = srv.Resources
For Each objResource in colResources
If objResource.Use r = strUSER Then objResource.rem ove(objResource .Name)
Next
I have seen various c# code using DirectoryServic es 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.Director yServices;

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

foreach(Directo ryEntry child in root.Children)
{
Console.WriteLi ne(child.Name);
}
}
}
}

Thanks for any insight or help with this problem
Rich
Nov 17 '05 #1
6 13314
Hi,

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

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

For Each objSession In objFSOps.Sessio ns
WScript.Echo objSession.Name
Next

For Each objResource In objFSOps.Resour ces
WScript.Echo objResource.Nam e
Next

May be, that helps.

Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:_m****** *******@manfbra un.de
(Remove the anti-spam-underscore to mail me!)

"Rich Crusco via DotNetMonster.c om" <fo***@nospam.D otNetMonster.co m> wrote
in message news:0c******** *************** *******@DotNetM onster.com...
I am looking for a way to convert the following vbs script to c#

Const ADS_SECURE_AUTH ENTICATION = 1
strADMIN = "adminaccou nt"
strPASSWORD = "password"
strUSER = "useraccoun t"
Set dso = GetObject("WinN T:")
Set srv = DSO.OpenDSObjec t("WinNT://servername/LanmanServer", strADMIN,
strPASSWORD, ADS_SECURE_AUTH ENTICATION)
Set colResources = srv.Resources
For Each objResource in colResources
If objResource.Use r = strUSER Then objResource.rem ove(objResource .Name) Next
I have seen various c# code using DirectoryServic es 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.Director yServices;

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

foreach(Directo ryEntry child in root.Children)
{
Console.WriteLi ne(child.Name);
}
}
}
}

Thanks for any insight or help with this problem
Rich

Nov 17 '05 #2

"Rich Crusco via DotNetMonster.c om" <fo***@nospam.D otNetMonster.co m> wrote
in message news:0c******** *************** *******@DotNetM onster.com...
I am looking for a way to convert the following vbs script to c#

Const ADS_SECURE_AUTH ENTICATION = 1
strADMIN = "adminaccou nt"
strPASSWORD = "password"
strUSER = "useraccoun t"
Set dso = GetObject("WinN T:")
Set srv = DSO.OpenDSObjec t("WinNT://servername/LanmanServer", strADMIN,
strPASSWORD, ADS_SECURE_AUTH ENTICATION)
Set colResources = srv.Resources
For Each objResource in colResources
If objResource.Use r = strUSER Then objResource.rem ove(objResource .Name)
Next
I have seen various c# code using DirectoryServic es 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.Director yServices;

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

foreach(Directo ryEntry child in root.Children)
{
Console.WriteLi ne(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")
{
IADsFileService Operations fso= container.Nativ eObject as
IADsFileService Operations;
if (fso != null)
{
foreach(IADsSes sion sess in fso.Sessions()) {
Console.WriteLi ne("Name : {0} \tUser: {1} \tComputer : {2}",
sess.Name, sess.User, sess.Computer);
}

IADsCollection resources = fso.Resources() as IADsCollection;
Console.WriteLi ne("----- Resource info -------");
foreach(IADsRes ource resource in resources)
{
try
{
Console.WriteLi ne("\tPath: {0}\tUser: {1}\tLockCount: {2}\tName:
{3}",
resource.Path, resource.User, resource.LockCo unt,
resource.Name);
}
catch (System.IO.File NotFoundExcepti on 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$******** ******@TK2MSFTN GP15.phx.gbl...

"Rich Crusco via DotNetMonster.c om" <fo***@nospam.D otNetMonster.co m> wrote
in message news:0c******** *************** *******@DotNetM onster.com...
I am looking for a way to convert the following vbs script to c#

Const ADS_SECURE_AUTH ENTICATION = 1
strADMIN = "adminaccou nt"
strPASSWORD = "password"
strUSER = "useraccoun t"
Set dso = GetObject("WinN T:")
Set srv = DSO.OpenDSObjec t("WinNT://servername/LanmanServer", strADMIN,
strPASSWORD, ADS_SECURE_AUTH ENTICATION)
Set colResources = srv.Resources
For Each objResource in colResources
If objResource.Use r = strUSER Then objResource.rem ove(objResource .Name) Next
I have seen various c# code using DirectoryServic es 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.Director yServices;

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

foreach(Directo ryEntry child in root.Children)
{
Console.WriteLi ne(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")
{
IADsFileService Operations fso= container.Nativ eObject as
IADsFileService Operations;
if (fso != null)
{
foreach(IADsSes sion sess in fso.Sessions()) {
Console.WriteLi ne("Name : {0} \tUser: {1} \tComputer : {2}",
sess.Name, sess.User, sess.Computer);
}

IADsCollection resources = fso.Resources() as IADsCollection;
Console.WriteLi ne("----- Resource info -------");
foreach(IADsRes ource resource in resources)
{
try
{
Console.WriteLi ne("\tPath: {0}\tUser: {1}\tLockCount: {2}\tName:
{3}",
resource.Path, resource.User, resource.LockCo unt,
resource.Name);
}
catch (System.IO.File NotFoundExcepti on 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.c om" <fo***@DotNetMo nster.com> wrote in
message news:46******** *************** *******@DotNetM onster.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.Nam e);

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
2034
by: Jeff Grundy | last post by:
How do I enumerate the machines domain, then enumerate the shares of a machine?
1
5969
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, AuthenticationTypes.Secure); DirectoryEntries sites = W3SVC.Children; DirectoryEntry newSite = sites.Add("1234","IIsWebServer"); //create a new site
8
6252
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 am using VB.net 2.0 any ideas? Here's my code Thanks in advance.
23
12341
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 working similar to this. Run this as a VBS script to see: Option Explicit Dim strUser, strMember, strDNSDomain, strContainer Dim objGroup,...
7
2921
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 error whenever I try to read properties of DirectoryEntry object. I had same problems when I was using WMI, but there setting ConnectionOptions co...
2
3407
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 properly. But when put on Web Server and try to access it from client machines, it is giving the Error as "An Operation Error Occured".
6
4069
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, having been advised by several luminaries that that is a "good thing to do"... :-) However, I'm experiencing a problem with the IEnumerable...
8
5023
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 have to do a readlines() first to get it into a list eg d = open("file").readlines()
2
1875
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 want to warn the user BEFORE he/she tries to start the whole process if any directory cannot be moved. The ideal situation is to tell the user the...
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.