Windows任务管理 连接用户登录信息 通用类[C#版]

简介:

通用类名[ComputerLoginUserInfo.cs] 代码如下:

复制代码
ExpandedBlockStart.gif 代码
 
  
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/vipstone/archive/2011/01/14/1935219.html,如需转载请自行联系原作者


目录
相关文章
|
4天前
|
开发框架 .NET C#
C#|.net core 基础 - 删除字符串最后一个字符的七大类N种实现方式
【10月更文挑战第9天】在 C#/.NET Core 中,有多种方法可以删除字符串的最后一个字符,包括使用 `Substring` 方法、`Remove` 方法、`ToCharArray` 与 `Array.Copy`、`StringBuilder`、正则表达式、循环遍历字符数组以及使用 LINQ 的 `SkipLast` 方法。
|
1月前
|
Windows
windows 电脑 连接蓝牙耳机没有麦克风
【8月更文挑战第31天】当Windows电脑连接蓝牙耳机后无法使用麦克风时,可尝试以下步骤解决:检查蓝牙设置,确保耳机正确连接并开启麦克风选项;检查音频设备设置,确认蓝牙耳机为默认播放和录制设备;更新蓝牙和音频驱动;确认耳机与系统的兼容性及正确设置。如问题未解,可重新配对耳机或联系客服。
1115 7
|
1月前
|
存储 C# 索引
C# 一分钟浅谈:数组与集合类的基本操作
【9月更文挑战第1天】本文详细介绍了C#中数组和集合类的基本操作,包括创建、访问、遍历及常见问题的解决方法。数组适用于固定长度的数据存储,而集合类如`List<T>`则提供了动态扩展的能力。文章通过示例代码展示了如何处理索引越界、数组长度不可变及集合容量不足等问题,并提供了解决方案。掌握这些基础知识可使程序更加高效和清晰。
64 2
|
1月前
|
SQL JavaScript 数据库
sqlite在Windows环境下安装、使用、node.js连接
sqlite在Windows环境下安装、使用、node.js连接
|
2天前
|
Java 程序员 C#
【类的应用】C#应用之派生类构造方法给基类构造方法传参赋值
【类的应用】C#应用之派生类构造方法给基类构造方法传参赋值
6 0
|
17天前
|
SQL 网络协议 数据库连接
已解决:连接SqlServer出现 provider: Shared Memory Provider, error: 0 - 管道的另一端上无任何进程【C#连接SqlServer踩坑记录】
本文介绍了解决连接SqlServer时出现“provider: Shared Memory Provider, error: 0 - 管道的另一端上无任何进程”错误的步骤,包括更改服务器验证模式、修改sa用户设置、启用TCP/IP协议,以及检查数据库连接语句中的实例名是否正确。此外,还解释了实例名mssqlserver和sqlserver之间的区别,包括它们在默认设置、功能和用途上的差异。
|
1月前
|
C# 数据安全/隐私保护
C# 一分钟浅谈:类与对象的概念理解
【9月更文挑战第2天】本文从零开始详细介绍了C#中的类与对象概念。类作为一种自定义数据类型,定义了对象的属性和方法;对象则是类的实例,拥有独立的状态。通过具体代码示例,如定义 `Person` 类及其实例化过程,帮助读者更好地理解和应用这两个核心概念。此外,还总结了常见的问题及解决方法,为编写高质量的面向对象程序奠定基础。
17 2
|
2月前
|
Java 应用服务中间件 开发工具
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
|
2月前
|
Java Windows
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
|
2月前
|
消息中间件 Java Kafka
【Azure 事件中心】在Windows系统中使用 kafka-consumer-groups.bat 查看Event Hub中kafka的consumer groups信息
【Azure 事件中心】在Windows系统中使用 kafka-consumer-groups.bat 查看Event Hub中kafka的consumer groups信息