Exemple #1
0
        /// <summary>
        /// 获取TS用户回话列表
        /// </summary>
        /// <returns></returns>
        public static List <LogonUser> GetLogonUserList()
        {
            List <LogonUser> LogonUsers = null;

            #region 查询代码
            WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration();
            LogonUser          cum          = null;
            LogonUsers = new System.Collections.Generic.List <LogonUser>();
            for (int i = 0; i < pSessionInfo.Length; i++)
            {
                if ("RDP-Tcp" != pSessionInfo[i].pWinStationName)
                {
                    try
                    {
                        int           count         = 0;
                        IntPtr        buffer        = IntPtr.Zero;
                        StringBuilder userName      = new StringBuilder();
                        StringBuilder clientUser    = new StringBuilder();
                        StringBuilder stateType     = new StringBuilder();
                        byte[]        protocalType  = new byte[2];
                        byte[]        connState     = new byte[1];
                        StringBuilder clientAddress = new StringBuilder();

                        bool userNameBool      = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSUserName, out userName, out count);
                        bool clientUserBool    = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientName, out clientUser, out count);
                        bool stateTypeBool     = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSWinStationName, out stateType, out count);
                        bool protocalTypeBool  = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientProtocolType, out protocalType, out count);
                        bool connStateBool     = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSConnectState, out connState, out count);
                        bool clientAddressBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientAddress, out clientAddress, out count);

                        if (userNameBool && clientUserBool && stateTypeBool & connStateBool)
                        {
                            cum                = new LogonUser();
                            cum.SessionId      = pSessionInfo[i].SessionID;
                            cum.UserName       = userName.ToString();
                            cum.ClientUserName = clientUser.ToString();
                            cum.SessionType    = stateType.ToString();
                            cum.ProtocalType   = (Silmoon.Windows.Systems.LogonUser.ClientProtocalType)((int)protocalType[0]);
                            cum.ConnectState   = (WTS_CONNECTSTATE_CLASS)connState[0];

                            WTS_CLIENT_ADDRESS ad = new WTS_CLIENT_ADDRESS();


                            //var aa = clientAddress[1];
                        }
                        LogonUsers.Add(cum);
                    }
                    catch
                    {
                    }
                }
            }
            #endregion
            return(LogonUsers);
        }
    public ComputerLoginUserInfo()
    {
        #region 查询代码
        TSControl.WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration();
        ComputerLoginUserInfoModel   cum          = null;
        ComputerLoginUserInfoList = new System.Collections.Generic.List <ComputerLoginUserInfoModel>();
        for (int i = 0; i < pSessionInfo.Length; i++)
        {
            if ("RDP-Tcp" != pSessionInfo[i].pWinStationName)
            {
                try
                {
                    int           count      = 0;
                    IntPtr        buffer     = IntPtr.Zero;
                    StringBuilder userName   = new StringBuilder();         // 用户名
                    StringBuilder clientUser = new StringBuilder();         // 客户端名
                    StringBuilder stateType  = new StringBuilder();         // 会话类型

                    bool userNameBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero,
                                                                             pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSUserName,
                                                                             out userName, out count);
                    bool clientUserBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero,
                                                                               pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSClientName,
                                                                               out clientUser, out count);
                    bool stateTypeBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero,
                                                                              pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSWinStationName,
                                                                              out stateType, out count);
                    if (userNameBool && clientUserBool && stateTypeBool)
                    {
                        cum                = new ComputerLoginUserInfoModel();
                        cum.UserName       = userName.ToString();
                        cum.ClientUserName = clientUser.ToString();
                        cum.SessionType    = stateType.ToString();
                    }
                    ComputerLoginUserInfoList.Add(cum);
                }
                catch (Exception ex) { }
            }
        }
        #endregion
    }