- 浏览: 72946 次
- 性别:
- 来自: 厦门
最新评论
-
BlueBing:
linyuliang 写道BlueBing 写道我想再做些修改 ...
IBatis Abator去除注释版 -
linyuliang:
BlueBing 写道我想再做些修改 可否提供下源码呢晚上我回 ...
IBatis Abator去除注释版 -
linyuliang:
xiaohu7924 写道为什么我用这个生成的名字不是你说的驼 ...
IBatis Abator去除注释版 -
xiaohu7924:
为什么我用这个生成的名字不是你说的驼锋名字,还是老样子
IBatis Abator去除注释版 -
BlueBing:
我想再做些修改 可否提供下源码呢
IBatis Abator去除注释版
using System;
using System.Management;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Text;
namespace TSConsoleApplication
{
/**/
/// <summary>
/// VS2005专业教程网收集整理,http://www.vs2005.com/
/// </summary>
public class TSControl
{
/**/
/// <summary>
/// Terminal Services API Functions,The WTSEnumerateSessions function retrieves a list of sessions on a specified terminal server,
/// </summary>
/// <param name="hServer">[in] Handle to a terminal server. Specify a handle opened by the WTSOpenServer function, or specify WTS_CURRENT_SERVER_HANDLE to indicate the terminal server on which your application is running</param>
/// <param name="Reserved">Reserved; must be zero</param>
/// <param name="Version">[in] Specifies the version of the enumeration request. Must be 1. </param>
/// <param name="ppSessionInfo">[out] Pointer to a variable that receives a pointer to an array of WTS_SESSION_INFO structures. Each structure in the array contains information about a session on the specified terminal server. To free the returned buffer, call the WTSFreeMemory function.
/// To be able to enumerate a session, you need to have the Query Information permission.</param>
/// <param name="pCount">[out] Pointer to the variable that receives the number of WTS_SESSION_INFO structures returned in the ppSessionInfo buffer. </param>
/// <returns>If the function succeeds, the return value is a nonzero value. If the function fails, the return value is zero</returns>
[DllImport("wtsapi32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool WTSEnumerateSessions(int hServer, int Reserved, int Version, ref long ppSessionInfo, ref int pCount);
/**/
/// <summary>
/// Terminal Services API Functions,The WTSFreeMemory function frees memory allocated by a Terminal Services function.
/// </summary>
/// <param name="pMemory">[in] Pointer to the memory to free</param>
[DllImport("wtsapi32.dll")]
public static extern void WTSFreeMemory(System.IntPtr pMemory);
/**/
/// <summary>
/// Terminal Services API Functions,The WTSLogoffSession function logs off a specified Terminal Services session.
/// </summary>
/// <param name="hServer">[in] Handle to a terminal server. Specify a handle opened by the WTSOpenServer function, or specify WTS_CURRENT_SERVER_HANDLE to indicate the terminal server on which your application is running. </param>
/// <param name="SessionId">[in] A Terminal Services session identifier. To indicate the current session, specify WTS_CURRENT_SESSION. You can use the WTSEnumerateSessions function to retrieve the identifiers of all sessions on a specified terminal server.
/// To be able to log off another user's session, you need to have the Reset permission </param>
/// <param name="bWait">[in] Indicates whether the operation is synchronous.
/// If bWait is TRUE, the function returns when the session is logged off.
/// If bWait is FALSE, the function returns immediately.</param>
/// <returns>If the function succeeds, the return value is a nonzero value.
/// If the function fails, the return value is zero.</returns>
[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
}
/**/
/// <summary>
/// The WTS_CONNECTSTATE_CLASS enumeration type contains INT values that indicate the connection state of a Terminal Services session.
/// </summary>
public enum WTS_CONNECTSTATE_CLASS
{
WTSActive,
WTSConnected,
WTSConnectQuery,
WTSShadow,
WTSDisconnected,
WTSIdle,
WTSListen,
WTSReset,
WTSDown,
WTSInit,
}
/**/
/// <summary>
/// The WTS_SESSION_INFO structure contains information about a client session on a terminal server.
/// if the WTS_SESSION_INFO.SessionID==0, it means that the SESSION is the local logon user's session.
/// </summary>
public struct WTS_SESSION_INFO
{
public int SessionID;
[MarshalAs(UnmanagedType.LPTStr)]
public string pWinStationName;
public WTS_CONNECTSTATE_CLASS state;
}
/**/
/// <summary>
/// The SessionEnumeration function retrieves a list of WTS_SESSION_INFO on a current terminal server.
/// </summary>
/// <returns>a list of WTS_SESSION_INFO on a current terminal server</returns>
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;
}
public TSControl()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
/**/
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/**/
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
TSControl.WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration();
for (int i = 0; i < pSessionInfo.Length; i++)
{
if (pSessionInfo[i].SessionID != 0)
{
try
{
int count = 0;
IntPtr buffer = IntPtr.Zero;
StringBuilder sb = new StringBuilder();
bool bsuccess = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSUserName, out sb, out count);
if (bsuccess)
{
//如果用户名为Administrator,则注销它。您可以通过改变Administrator注销其它的用户
if (sb.ToString().Trim() == "Administrator")
{
while (TSControl.WTSLogoffSession(0, pSessionInfo[i].SessionID, true))
{
System.Threading.Thread.Sleep(3000);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Console.ReadLine();
}
}
}
using System.Management;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Text;
namespace TSConsoleApplication
{
/**/
/// <summary>
/// VS2005专业教程网收集整理,http://www.vs2005.com/
/// </summary>
public class TSControl
{
/**/
/// <summary>
/// Terminal Services API Functions,The WTSEnumerateSessions function retrieves a list of sessions on a specified terminal server,
/// </summary>
/// <param name="hServer">[in] Handle to a terminal server. Specify a handle opened by the WTSOpenServer function, or specify WTS_CURRENT_SERVER_HANDLE to indicate the terminal server on which your application is running</param>
/// <param name="Reserved">Reserved; must be zero</param>
/// <param name="Version">[in] Specifies the version of the enumeration request. Must be 1. </param>
/// <param name="ppSessionInfo">[out] Pointer to a variable that receives a pointer to an array of WTS_SESSION_INFO structures. Each structure in the array contains information about a session on the specified terminal server. To free the returned buffer, call the WTSFreeMemory function.
/// To be able to enumerate a session, you need to have the Query Information permission.</param>
/// <param name="pCount">[out] Pointer to the variable that receives the number of WTS_SESSION_INFO structures returned in the ppSessionInfo buffer. </param>
/// <returns>If the function succeeds, the return value is a nonzero value. If the function fails, the return value is zero</returns>
[DllImport("wtsapi32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool WTSEnumerateSessions(int hServer, int Reserved, int Version, ref long ppSessionInfo, ref int pCount);
/**/
/// <summary>
/// Terminal Services API Functions,The WTSFreeMemory function frees memory allocated by a Terminal Services function.
/// </summary>
/// <param name="pMemory">[in] Pointer to the memory to free</param>
[DllImport("wtsapi32.dll")]
public static extern void WTSFreeMemory(System.IntPtr pMemory);
/**/
/// <summary>
/// Terminal Services API Functions,The WTSLogoffSession function logs off a specified Terminal Services session.
/// </summary>
/// <param name="hServer">[in] Handle to a terminal server. Specify a handle opened by the WTSOpenServer function, or specify WTS_CURRENT_SERVER_HANDLE to indicate the terminal server on which your application is running. </param>
/// <param name="SessionId">[in] A Terminal Services session identifier. To indicate the current session, specify WTS_CURRENT_SESSION. You can use the WTSEnumerateSessions function to retrieve the identifiers of all sessions on a specified terminal server.
/// To be able to log off another user's session, you need to have the Reset permission </param>
/// <param name="bWait">[in] Indicates whether the operation is synchronous.
/// If bWait is TRUE, the function returns when the session is logged off.
/// If bWait is FALSE, the function returns immediately.</param>
/// <returns>If the function succeeds, the return value is a nonzero value.
/// If the function fails, the return value is zero.</returns>
[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
}
/**/
/// <summary>
/// The WTS_CONNECTSTATE_CLASS enumeration type contains INT values that indicate the connection state of a Terminal Services session.
/// </summary>
public enum WTS_CONNECTSTATE_CLASS
{
WTSActive,
WTSConnected,
WTSConnectQuery,
WTSShadow,
WTSDisconnected,
WTSIdle,
WTSListen,
WTSReset,
WTSDown,
WTSInit,
}
/**/
/// <summary>
/// The WTS_SESSION_INFO structure contains information about a client session on a terminal server.
/// if the WTS_SESSION_INFO.SessionID==0, it means that the SESSION is the local logon user's session.
/// </summary>
public struct WTS_SESSION_INFO
{
public int SessionID;
[MarshalAs(UnmanagedType.LPTStr)]
public string pWinStationName;
public WTS_CONNECTSTATE_CLASS state;
}
/**/
/// <summary>
/// The SessionEnumeration function retrieves a list of WTS_SESSION_INFO on a current terminal server.
/// </summary>
/// <returns>a list of WTS_SESSION_INFO on a current terminal server</returns>
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;
}
public TSControl()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
/**/
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/**/
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
TSControl.WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration();
for (int i = 0; i < pSessionInfo.Length; i++)
{
if (pSessionInfo[i].SessionID != 0)
{
try
{
int count = 0;
IntPtr buffer = IntPtr.Zero;
StringBuilder sb = new StringBuilder();
bool bsuccess = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSUserName, out sb, out count);
if (bsuccess)
{
//如果用户名为Administrator,则注销它。您可以通过改变Administrator注销其它的用户
if (sb.ToString().Trim() == "Administrator")
{
while (TSControl.WTSLogoffSession(0, pSessionInfo[i].SessionID, true))
{
System.Threading.Thread.Sleep(3000);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Console.ReadLine();
}
}
}
发表评论
-
实现GridView控件的删除多条记录功能系列(1)
2005-08-10 14:15 1450在Asp.Net 2.0中新增的控件GridView可 ... -
实现GridView控件的删除多条记录功能系列(2)
2005-08-10 14:55 1299在上一篇中,我们已经开发了需要的CheckBox控件, ... -
实现GridView控件的删除多条记录功能系列(3)
2005-08-11 12:18 1321本篇将讲述如何解决GridView控件中使用Check ... -
[转]C#正则表达式小结
2007-09-24 11:38 1209地址: http://www.cnblogs.co ... -
[转]全面剖析C#正则表达式
2007-09-24 11:41 1378地址:http://fineboy.cnblogs.com/ ... -
(转贴)VS.NET下水晶报表分发时的问题及解决
2007-10-18 17:27 1169一、载入报表时报错 ... -
(转贴)图解使用VS.NET部署含水晶报表的网站
2007-10-18 17:56 2126Crystal Report ,中文名称“水晶报表”,因为做报 ... -
[转]VS2005 web程序自定义安装包的制作
2007-10-23 10:28 2113利用VS2005的“Web安装项目”建立安装包很难对安装过程进 ... -
[转]使用C#进行点对点通讯和文件传输(通讯基类部分)
2007-10-24 10:59 907最近一个项目要用到点对点文件传输,俺就到处找资料写程序,最后终 ... -
[转]使用C#进行点对点通讯和文件传输(发送接收部分)
2007-10-24 11:00 1657上面介绍了通讯的基类,下面就是使用那个类进行发送和接收的部分: ... -
[转载]C#中串口通信编程
2007-10-24 11:01 4502原文及源代码位置:http://bbs.msproject.c ... -
[转]C#多线程编程实例实战
2007-10-24 11:02 1667单个写入程序/多个阅读程序在.Net类库中其实已经提供了实现, ... -
[转]实现同时只允许运行一个程序实例
2007-10-24 11:06 1129方法一: /// <summary> /// 从这 ... -
[转]IIS虚拟目录控制类
2007-10-24 11:08 447using System;using System.Data; ... -
[转]C#的usb通讯编程
2007-10-24 11:10 2810using System;using System.Colle ... -
[转]IIS站点管理类
2007-10-24 11:10 951using System;using System.Colle ... -
[转]使用.NET实现断点续传
2007-10-24 11:11 1319断点续传的原理 在了解HTTP断点续传的原理之前,先来说说HT ... -
[转]Datagridview 实现二维表头
2007-10-26 16:42 2338最近把我们的b/s系统,增加智能客户端的功能。确实智能客户端是 ... -
[转]Web项目下NHibernate的Session管理的解决方案
2007-11-07 10:47 3390NHibernate的Session的管理一直是个问题,在系统 ... -
[转] NHibernate对像版本控制使用示例
2007-11-07 10:49 1252<version name="Version& ...
相关推荐
在C#编程中,有时我们需要实现一些自动化任务,比如定时关闭、注销或重启计算机。这在系统维护、软件测试或者用户交互场景下非常有用。本文将深入探讨如何使用C#来实现这些功能。 首先,我们要了解Windows API在C#...
客户端则负责发起连接请求,控制远程桌面,实现用户登录、注销和切换。C#中的`System.Net`和`System.IO.Ports`类库可以帮助我们建立网络连接。客户端可能需要提供用户界面,让用户输入目标计算机的地址、用户名、...
本项目“远程关机注销程序C#版”是一个利用C#编写的实用工具,能够实现对远程计算机的关机或注销操作,这对于系统管理员进行远程管理或者故障排查非常有用。下面将详细介绍这个程序的相关知识点。 首先,要实现远程...
在本篇文章中,我们将探讨如何使用 C# 来实现 Windows 操作系统的几种常见操作:自动关机、重启、注销、待机以及休眠。这些功能通常通过 Windows API 函数实现,而 C# 作为 .NET Framework 的一部分,提供了对底层 ...
本主题聚焦于C#实现计算机远程操作的技术,主要包括服务端控制与客户端执行两个核心部分,涉及到的功能有远程重启、注销和关闭计算机。下面我们将深入探讨这些知识点。 首先,让我们了解C#中的远程操作基础。远程...
本实例源码涉及的是利用C#实现远程控制计算机的功能,包括远程关闭和重启。这通常涉及到网络操作,如套接字编程和Windows API调用。 首先,我们需要理解C#中的网络编程基础。C#提供了System.Net命名空间,其中包含...
1. **远程注销**:注销远程计算机通常涉及到调用WMI(Windows Management Instrumentation)服务。通过创建一个ManagementScope对象,设置目标计算机的连接参数,然后使用ManagementObjectSearcher查询相关的WMI类,...
在C#编程中,系统级别的操作如关机、注销和重启是通过Windows API(应用程序接口)调用来实现的。Windows API提供了丰富的函数,允许开发者执行这些任务,而不仅仅是用户界面操作。下面我们将深入探讨如何在C#中实现...
1. **引用本机库**: 尽管WinBio API是本机API,但C#项目可以直接引用`System.Runtime.InteropServices`命名空间,使用DllImport特性来声明和调用本机函数。 2. **定义数据结构**: WinBio API使用了一些特定的数据...
注销会结束当前用户的会话,但不会关闭系统,其他用户仍然可以登录。 4. **兼容性问题**: 标签中提到"在XP下成功,其他请自测",意味着这些函数可能在Windows XP系统上经过验证有效,但在其他版本的Windows系统...
在C# 开发过程中,实现关机、重启或注销等系统级操作是常见的需求之一。然而,在某些特定环境下,如远程控制场景下,这些操作可能会遭遇权限不足的问题。本文将详细介绍在C# 中执行这类系统操作时可能遇到的权限问题...
在C#中,可以通过P/Invoke技术调用这些API函数,如SetWindowsHookEx()来安装钩子,UnhookWindowsHookEx()来卸载钩子,以及CallNextHookEx()来传递钩子链中的事件。键盘钩子的工作原理是,当一个键盘事件发生时,系统...
这个“小程序_实现关机注销重启关闭打开显示器”是基于WinformApp程序,它是一个用C#或Visual Basic .NET等.NET Framework支持的语言编写的桌面应用。下面将详细讨论相关的知识点: 1. **WinformApp**: Winform...
本话题主要探讨了如何使用C#语言来实现客户端对服务端的一些高级功能,包括远程关机、结束进程、运行程序、打开进程以及执行DOS命令。这些功能在远程管理系统或者自动化运维场景中具有广泛应用。 首先,让我们详细...
【勾月关机系统(c#代码编写)】是一款利用C#编程语言开发的系统工具,主要功能包括系统关机、重启、注销以及定时关机。C#是一种面向对象的编程语言,由微软公司开发,广泛应用于Windows平台上的应用程序开发。在这款...
这需要深入理解网络数据传输原理,以及可能涉及的API调用,如WebClient或HttpClient类来获取远程数据。此外,系统可能还需要处理各种网络异常,确保数据获取的稳定性。 此外,系统还提供了余额实时监控功能,这对于...
[C#/WPF]Echo.Net C#远程控制(服务端、客户端) 主要功能有屏幕监控、鼠标键盘控制、任务管理器、Telnet、系统信息查看、关机注销重启等 ...熟悉了XML的序列化、反序列化,Socket通讯,Win32API调用等
例如,调用`kernel32.dll`库中的`ExitWindowsEx`函数来实现关机和注销,`kernel32.dll`中的`InitiateSystemShutdown`函数可以用来远程关机,而`kernel32.dll`的`ExitProcess`配合`user32.dll`的`ExitWindows`可以...
这个小程序对于初学者来说尤其有价值,因为它展示了如何利用C#的基本语法和Windows API调用来实现系统级别的操作。 【描述】:描述中提到的“快速关机”意味着该程序设计简洁高效,能够在用户点击后迅速执行关机...
如果是一个源代码项目,那么开发者可以打开查看具体的实现细节,包括如何组织代码,如何调用API,以及如何处理不同操作的逻辑流程。如果是可执行文件,则可以直接运行体验这些功能。 总的来说,快速关机小程序(VC版...