日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2025/3/11 C# 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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]?

?

總結

以上是生活随笔為你收集整理的Windows任务管理 连接用户登录信息 通用类[C#版]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。