通用类名[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]

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

  1. 服务器远程登录用户在哪查看,RAKsmart VNC用户登录信息在哪查看?

    前面跟大家分享了RAKsmart Windows美国服务器远程连接VNC方法,不过很多小伙伴在用客户端连接远程服务器时,对用户登录信息不知道怎么填写,也不知道在哪里查看.这里就给大家介绍下. 1.首先 ...

  2. Day212.OAuth2、微信二维码登入注册功能、用户登录信息前后端供、讲师列表前后端 -谷粒学院

    谷粒学院 OAuth2的使用场景 一.OAuth2解决什么问题 1.OAuth2提出的背景 照片拥有者想要在云冲印服务上打印照片,云冲印服务需要访问云存储服务上的资源 2.图例 资源拥有者:照片拥有者 ...

  3. 通过cookie保存并读取用户登录信息

    浏览器将cookie以key/value的形式保存到客户机的某个指定目录 通过cookie的getCookies()方法可获取所有cookie对象的集合 通过cookie对象的getName()方法可 ...

  4. 如何在redhat8里使用gcc命令_如何使用who命令检查用户登录信息

    请关注本头条号,每天坚持更新原创干货技术文章. 如需学习视频,请在微信搜索公众号"智传网优"直接开始自助视频学习 1. 前言 本教程主要介绍如何使用who命令检查用户登录信息. 如 ...

  5. 通过cookie保存并读取用户登录信息实例

    通过cookie的getCookies()方法可获取所有cookie对象的集合:通过getName()方法可以获取指定的名称的cookie:通过getValue()方法获取到cookie对象的值.另外 ...

  6. 计算机禁止用户登录,win7系统禁止显示用户登录信息的设置方法

    win7系统功能强大,有着xp系统不可比拟的其他功能,不过有些功能虽然强大,但为了保障系统安全信息,最好还是禁用掉为好.比如当用户登录系统桌面时,可以查看上次具体登录情况,包括时间和使用账号.虽然此功 ...

  7. 使用JSP实现简单的登录注册功能,并且使用Session跟踪用户登录信息

    使用JSP实现简单的登录注册功能,并且使用了Session来跟踪用户的登录信息,这个是用纯JSP来实现此功能的,由于没有连接数据库,所以使用List来模拟数据库. 第一步:创建web项目 如果有不会创 ...

  8. 【安卓】基于SharedPreferences实现用户登录信息的存储

    1. 设计登录页面 比较简单,重要的点就一个,有一个记住密码的按钮. 代码实现 <?xml version="1.0" encoding="utf-8"? ...

  9. oracle通过sid远程登录,oracle怎么捕获用户登录信息,如SID,IP地址等

    可以利用登录触发器,如 CREATE OR REPLACE TRIGGER tr_login_record AFTER logon ON DATABASE DECLARE miUserSid NUMB ...

最新文章

  1. 用开满鲜花的情怀对待每一份求知欲
  2. Java学习之if语句
  3. 打造轻量化的View Controller
  4. 微信创始人张小龙的创业故事
  5. Atitit.提升api兼容性的方法 v3 q326
  6. UVA-1515 Pool construction (最小割)
  7. 太强了!Scikit-learn 0.22新版本发布,新功能更加方便
  8. Spark笔记:复杂RDD的API的理解(下)
  9. Python附带了大量的库 - 初学者如何学起
  10. 1019. General Palindromic Number (20)
  11. Leetcode: Pascal's Triangle II
  12. mysql两个下拉框_两个下拉框的纠葛
  13. SpringClould之Eureka原理详解
  14. 最强内存稳定性测试软件,内存稳定性测试软件Memtest正式版——HyperX FURY DDR4成绩喜人...
  15. 软件测试工程师经典面试题
  16. python画图——雪花(科赫曲线)
  17. 机器学习实验一: 简易knn模型
  18. 获取位置geolocation 加速度devicemotion 角度deviceorientation
  19. WPS中添加页眉和页脚
  20. 【CentOS8.0开启防火墙放行8081端口】

热门文章

  1. oracle导入impdp
  2. Spark初识-弹性分布式数据集RDD
  3. Angular实现dialog对话框封装
  4. 内存缓存LruCache实现原理
  5. PHP5.3.8 + Mysql5.5.18 + Nginx1.1.8 安装使用感受
  6. 女人,向《奋斗》中的夏琳米莱们学习什么
  7. QQ2007 Beta2 下载地址泄露
  8. python类的编写模板_python开发笔记-类
  9. python random()*10的值不可能是_Python
  10. linux查看当前用户终端,Linux----基本命令的使用(vi命令,查看文件内容,显示进程,切换用户等)...