我要用C#获取任务管理器-用户列表,以及右击用户实现断开,注销
有源码的最好。谢谢
谢谢谢谢
有源码的最好。谢谢
谢谢谢谢
解决方案 »
- 程序已经交给用户使用,后来又修改了数据库结构
- 用HyperLink和DropDown实现DropDown内容改变,HyperLink值也改变,跳转的页面也随之改变,不知道该怎么实现?
- 考勤机动态链接库的 函数 求指点!
- sqlserver 如何生成一个字母加数字组合唯一的编号
- 请问什么是MDI容器,它与一般的WINDOWS窗体有什么区别,用它有什么好处,什么时候用
- 请问: 如何为dataset中的datatable创建视图?
- c#订阅事件是用了Lambda,怎么取消订阅呢?
- Excel.ApplicationClass的问题,找不到excel空间!
- !!!在C#中,如何自动产生递增的自然数序列数,而且只加不减???
- SMTP发送mail接收延迟太长?
- 请问,如何在程序所在目录下的某个文件夹下新建一个文件夹
- 控制台和WINFORM通信问题
通用类名[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);
}
不可以,你需要再找新的api
可以用 Porcess 类 运行 cmd 执行 qwinsta.exe 返回输出结果 就可以了private void button1_Click(object sender, EventArgs e)
{
// 调用代码
string[] cmd = new string[] { "qwinsta.exe" };
MessageBox.Show(Cmd(cmd));
CloseProcess("cmd.exe");
}/// <summary>
/// 运行CMD命令
/// </summary>
/// <param name="cmd">命令</param>
/// <returns></returns>
public static string Cmd(string [] cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i++)
{
p.StandardInput.WriteLine(cmd[i].ToString());
}
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return strRst;
}
呵呵,谢谢。这个之前不会。刚刚弄出来。
有DOS命令,可以 断开远程连接用户的吗。
就是我一台服务器上,有很多个用户远程连接上来。 我想传IP或者用户名,用DOS命令断开用户。
http://blog.csdn.net/summonyyq/article/details/5649747