Windows任务管理 连接用户登录信息 通用类[C#版]
通用类名[ComputerLoginUserInfo.cs] 代码如下:

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]
关注下面二维码,订阅更多精彩内容。

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验