Drop users connected to my PC?

P

pnp

Hi all,
from computer management|shared folders|sessions I can see all the
users in a network that are connected to my PC and how many open files
they have. Is there a way to monitor this through C# and drop the users
that I want?

thanks in advance,
Peter
 
W

Willy Denoyette [MVP]

Why do you wan't your users to connect if at a certain moment you want to
get rid of them?
Don't forget that if they have files open, chances are that these become
corrupted. Is this realy what you want?

Willy.
 
W

Willy Denoyette [MVP]

No this has nothing to do with OP's question, what he wan't is to disconnect
a user from a share.

Willy.
 
W

Willy Denoyette [MVP]

pnp said:
Yes that is what I want to do. How can I accomplish that?


One possibility is to use System.Management classes and the ServerSession
and ServerConnection WMI classes.
Note that:
- the client cannot be stopped to re-open another session, this can happen
automatically, for instance office applications will re-initiate and re-open
the files it has currently open.
- all resources associated with this client session will be closed (files
are close), this guarantess physical consistency, but not logical
consistancy.

Herewith a small sample:

using System;
using System.Management;
class App {
public static void Main() {
// remove session for user 'SomeUser'
DropThisSession(@"SomeUser");
}

static void DropThisSession(string objectQry) {
SelectQuery query = new SelectQuery("select ComputerName, UserName,
ResourcesOpened from win32_serversession where username ='" + objectQry
+"'" );
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query)) {
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine("Session opened by: {0} from {1} with {2} resources
opened",
mo.Properties["UserName"].Value,
mo.Properties["ComputerName"].Value,
mo.Properties["ResourcesOpened"].Value);
// Optionaly - Get associated serverconnection instance
foreach (ManagementBaseObject b in
mo.GetRelated("Win32_ServerConnection")) {
ShowServerConnectionProperties(b.ToString());
}
// Delete the session, this will close all opened resources (files etc..)
for this session
mo.Delete();
}
}
}
static void ShowServerConnectionProperties(string objectClass) {
using(ManagementObject process = new ManagementObject (objectClass))
{
process.Get();
PropertyDataCollection processProperties = process.Properties;
Console.WriteLine("ConnectionID: {0,6} \tShareName: {1}" ,
processProperties["ConnectionID"].Value,
processProperties["ShareName"].Value);
}
}


Willy.
 
P

pnp

Thank you for your help Willy. I believe that this will work for me.

Yes that is what I want to do. How can I accomplish that?



One possibility is to use System.Management classes and the ServerSession
and ServerConnection WMI classes.
Note that:
- the client cannot be stopped to re-open another session, this can happen
automatically, for instance office applications will re-initiate and re-open
the files it has currently open.
- all resources associated with this client session will be closed (files
are close), this guarantess physical consistency, but not logical
consistancy.

Herewith a small sample:

using System;
using System.Management;
class App {
public static void Main() {
// remove session for user 'SomeUser'
DropThisSession(@"SomeUser");
}

static void DropThisSession(string objectQry) {
SelectQuery query = new SelectQuery("select ComputerName, UserName,
ResourcesOpened from win32_serversession where username ='" + objectQry
+"'" );
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query)) {
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine("Session opened by: {0} from {1} with {2} resources
opened",
mo.Properties["UserName"].Value,
mo.Properties["ComputerName"].Value,
mo.Properties["ResourcesOpened"].Value);
// Optionaly - Get associated serverconnection instance
foreach (ManagementBaseObject b in
mo.GetRelated("Win32_ServerConnection")) {
ShowServerConnectionProperties(b.ToString());
}
// Delete the session, this will close all opened resources (files etc..)
for this session
mo.Delete();
}
}
}
static void ShowServerConnectionProperties(string objectClass) {
using(ManagementObject process = new ManagementObject (objectClass))
{
process.Get();
PropertyDataCollection processProperties = process.Properties;
Console.WriteLine("ConnectionID: {0,6} \tShareName: {1}" ,
processProperties["ConnectionID"].Value,
processProperties["ShareName"].Value);
}
}


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top