public static List<SessionInfo> ListSessions(string serverName, string userName, string domainName, string clientName, WTS_CONNECTSTATE_CLASS? state) { IntPtr server = IntPtr.Zero; List<SessionInfo> sessions = new List<SessionInfo>(); server = OpenServer(serverName); try { IntPtr ppSessionInfo = IntPtr.Zero; Int32 count = 0; Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count); Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO)); Int32 current = (int)ppSessionInfo; if (retval != 0) { for (int i = 0; i < count; i++) { SessionInfo sessionInfo = new SessionInfo(); WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO)); current += dataSize; sessionInfo.Id = si.SessionID; sessionInfo.UserName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSUserName); sessionInfo.DomainName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSDomainName); sessionInfo.ClientName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSClientName); sessionInfo.State = si.State; if (userName != null || domainName!=null || clientName != null || state!=null) //In this case, the caller is asking to return only matching sessions { if (userName != null && !String.Equals(userName, sessionInfo.UserName, StringComparison.CurrentCultureIgnoreCase)) continue; //Not matching if (clientName != null && !String.Equals(clientName, sessionInfo.ClientName, StringComparison.CurrentCultureIgnoreCase)) continue; //Not matching if (domainName != null && !String.Equals(domainName, sessionInfo.DomainName, StringComparison.CurrentCultureIgnoreCase)) continue; //Not matching if (state != null && sessionInfo.State != state.Value) continue; } sessions.Add(sessionInfo); } WTSFreeMemory(ppSessionInfo); } } finally { CloseServer(server); } return sessions; }
public static List <SessionInfo> ListSessions(string serverName, string userName, string domainName, string clientName, WTS_CONNECTSTATE_CLASS?state) { var server = IntPtr.Zero; var sessions = new List <SessionInfo>(); server = OpenServer(serverName); try { var ppSessionInfo = IntPtr.Zero; var count = 0; var retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count); var dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO)); var current = (int)ppSessionInfo; if (retval != 0) { for (var i = 0; i < count; i++) { var sessionInfo = new SessionInfo(); var si = (WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)current, typeof(WTS_SESSION_INFO)); current += dataSize; sessionInfo.Id = si.SessionID; sessionInfo.UserName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSUserName); sessionInfo.DomainName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSDomainName); sessionInfo.ClientName = QuerySessionInfo(server, sessionInfo.Id, WTS_INFO_CLASS.WTSClientName); sessionInfo.State = si.State; if (userName != null || domainName != null || clientName != null || state != null ) //In this case, the caller is asking to return only matching sessions { if (userName != null && !string.Equals(userName, sessionInfo.UserName, StringComparison.CurrentCultureIgnoreCase)) { continue; //Not matching } if (clientName != null && !string.Equals(clientName, sessionInfo.ClientName, StringComparison.CurrentCultureIgnoreCase)) { continue; //Not matching } if (domainName != null && !string.Equals(domainName, sessionInfo.DomainName, StringComparison.CurrentCultureIgnoreCase)) { continue; //Not matching } if (state != null && sessionInfo.State != state.Value) { continue; } } sessions.Add(sessionInfo); } WTSFreeMemory(ppSessionInfo); } } finally { CloseServer(server); } return(sessions); }