Windows任务管理 连接用户登录信息 通用类[C#版]
2011-01-14 09:50
393 查看
通用类名[ComputerLoginUserInfo.cs] 代码如下:
代码
使用:ComputerLoginUserInfo uif = new ComputerLoginUserInfo();
foreach (ComputerLoginUserInfoModel item in uif.ComputerLoginUserInfoList)
{
Response.Write(item.UserName);
}
ps:尊重劳动人民成果,技术是共享的,但如果引用代码请注明出处谢谢.
[引用地址:http://www.cnblogs.com/stone_w/archive/2011/01/14/1935219.html]
代码
using System; //---引用 using System.Runtime.InteropServices; using System.Text; /// <summary> /// Windows 任务管理器登录用户信息 /// author:Stone_W /// date:2011.1.14 /// </summary> public class ComputerLoginUserInfo { #region 本机连接用户信息API封装 public class TSControl { [DllImport("wtsapi32", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool WTSEnumerateSessions(int hServer, int Reserved, int Version, ref long ppSessionInfo, ref int pCount); [DllImport("wtsapi32.dll")] public static extern void WTSFreeMemory(System.IntPtr pMemory); [DllImport("wtsapi32.dll")] public static extern bool WTSLogoffSession(int hServer, long SessionId, bool bWait); [DllImport("Wtsapi32.dll")] public static extern bool WTSQuerySessionInformation( System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out StringBuilder ppBuffer, out int pBytesReturned); public enum WTSInfoClass { WTSInitialProgram, WTSApplicationName, WTSWorkingDirectory, WTSOEMId, WTSSessionId, WTSUserName, WTSWinStationName, WTSDomainName, WTSConnectState, WTSClientBuildNumber, WTSClientName, WTSClientDirectory, WTSClientProductId, WTSClientHardwareId, WTSClientAddress, WTSClientDisplay, WTSClientProtocolType } public enum WTS_CONNECTSTATE_CLASS { WTSActive, WTSConnected, WTSConnectQuery, WTSShadow, WTSDisconnected, WTSIdle, WTSListen, WTSReset, WTSDown, WTSInit, } public struct WTS_SESSION_INFO { public int SessionID; [MarshalAs(UnmanagedType.LPTStr)] public string pWinStationName; public WTS_CONNECTSTATE_CLASS state; } public static WTS_SESSION_INFO[] SessionEnumeration() { //Set handle of terminal server as the current terminal server int hServer = 0; bool RetVal; long lpBuffer = 0; int Count = 0; long p; WTS_SESSION_INFO Session_Info = new WTS_SESSION_INFO(); WTS_SESSION_INFO[] arrSessionInfo; RetVal = WTSEnumerateSessions(hServer, 0, 1, ref lpBuffer, ref Count); arrSessionInfo = new WTS_SESSION_INFO[0]; if (RetVal) { arrSessionInfo = new WTS_SESSION_INFO[Count]; int i; p = lpBuffer; for (i = 0; i < Count; i++) { arrSessionInfo[i] = (WTS_SESSION_INFO)Marshal.PtrToStructure(new IntPtr(p), Session_Info.GetType()); p += Marshal.SizeOf(Session_Info.GetType()); } WTSFreeMemory(new IntPtr(lpBuffer)); } else { //Insert Error Reaction Here } return arrSessionInfo; } } #endregion public System.Collections.Generic.List<ComputerLoginUserInfoModel> ComputerLoginUserInfoList; 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 } } public class ComputerLoginUserInfoModel { #region 用户信息字段 private string userName; private string clientUserName; private string sessionType; /// <summary> /// 会话类型 /// </summary> public string SessionType { get { return sessionType; } set { sessionType = value; } } /// <summary> /// 客户端名 /// </summary> public string ClientUserName { get { return clientUserName; } set { clientUserName = value; } } /// <summary> /// 登录用户名 /// </summary> public string UserName { get { return userName; } set { userName = value; } } #endregion }
使用:ComputerLoginUserInfo uif = new ComputerLoginUserInfo();
foreach (ComputerLoginUserInfoModel item in uif.ComputerLoginUserInfoList)
{
Response.Write(item.UserName);
}
ps:尊重劳动人民成果,技术是共享的,但如果引用代码请注明出处谢谢.
[引用地址:http://www.cnblogs.com/stone_w/archive/2011/01/14/1935219.html]
相关文章推荐
- Windows任务管理 连接用户登录信息 通用类[C#版]
- 基于数据库的C#产品信息管理系统,用户登录程序
- Sybase数据库在UNIX、Windows上的实施和管理 讨论6 显示某个登录用户的信息
- .net读取Windows登录用户信息(downmoon)
- c#中连接数据库关于sqlconnection的用户sa无法登录的问题
- (转)C#开发微信门户及应用(4)--关注用户列表及详细信息管理
- 【Android】用户登录注册界面开发及用户信息管理案例代码详解
- C#_会员管理系统:开发一(用户登录)
- windows无法连接到user profile service服务。此问题阻止标准用户登录系统
- JDBC练习题,用户信息管理,登录注册
- 利用字典管理用户的登录信息
- SQL错误:相关的信息为:用户 sa 登录失败。原因: 未与信任 SQL Server 连接相关联。
- 一些获取当前登录Windows系统用户信息的命令
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.4 Edit修改用户信息]
- C#开发微信门户及应用(4) 关注用户列表及详细信息管理
- [置顶] Struts2+Hibernate4开发学生信息管理功能--(三)用户登录模块
- C#窗体应用程序 vs + sql server2008连接举例,简单的学生信息管理系统
- [C#]连接带有密码的ACCESS数据库时出现“无法启动应用程序。工作组信息文件丢失,或是已被其它用户以独占方式打开”的解决方法
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户]
- windows 远程登录用户管理