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.