当前位置:首页 > 网络编程 > 软件语言 > .NET > .NET Lock, Logoff, Reboot, Shutdown, Hibernate, Standby

.NET Lock, Logoff, Reboot, Shutdown, Hibernate, Standby

点击次数:75 次 发布日期:2008-11-06 08:12:23 作者:源代码网
源代码网推荐
广告载入中

源代码网整理以下This article is about locking, logging off , rebooting, shutting down, hibernating and putting the system on stand by mode in .Net. Here we are going to use both unmanaged code and .Net framework for these functions.
Lock, Logoff, Reboot, Shutdown, Hibernate, Standby in .Net

源代码网整理以下Introduction

源代码网整理以下This article is about locking, logging off , rebooting, shutting down, hibernating and putting the system on stand by mode in .Net. Here we are going to use both unmanaged code and .Net framework for these functions.

源代码网整理以下Getting Started

源代码网整理以下Let us start by creating a windows application. To our newly created form, add seven buttons entitled btnLockComp, btnLogOff, btnReboot, btnShutdown, btnForceLogOff, btnHibernate, btnStandby.

源代码网整理以下

源代码网整理以下Lock Workstation

源代码网整理以下Let us start with locking the workstation, which is supposed to lock the current user session. We will call a windows API for doing this. For calling an un-managed piece of code, we need to add the System.Runtime.InteropServices namespace to the using directive.

源代码网整理以下
using System.Runtime.InteropServices;

源代码网整理以下Now we are ready to import the windows API library and define the function that we intend to use. The function to lock the workstation resides in the user32.dll library. And the function for locking the desktop is LockWorkStation. The following statements should be added to the class to import the library.

源代码网整理以下[DllImport("user32.dll")]

源代码网整理以下public static extern void LockWorkStation();

源代码网整理以下Next step is to double click the btnLockComp button to create a click event handler and call the LockWorkStation API to lock the workstation.

源代码网整理以下LockWorkStation();

源代码网整理以下Log Off

源代码网整理以下For logging off we are going to use an unmanaged API function called ExitWindowsEx(). This function accepts two arguments, one for flag(logoff, shutdown, reboot, etc.,) and the other for reason for this action(maintenance, software update, etc.,). Now import the user32.dll once again, this time so we can use the ExitWindowsEx() function, as shown below:

源代码网整理以下[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);

源代码网整理以下Double click the log off button and add the following function call to the event. Flag 0 indicates logoff, 软件开发网 www.mscto.com

源代码网整理以下ExitWindowsEx(0, 0);

源代码网整理以下To force processes to terminate while logging off, change the flag to 4 in the function as below

源代码网整理以下ExitWindowsEx(4, 0);

源代码网整理以下Reboot

源代码网整理以下To reboot we are going to use the same function ExitWindowsEx but with a different flag. Add the following code to the click event handler of the reboot button.

源代码网整理以下ExitWindowsEx(2, 0);

源代码网整理以下
Shutdown

源代码网整理以下Now add the following code to the button Shutdown"s click event handler.

源代码网整理以下ExitWindowsEx(1, 0);

源代码网整理以下
Hibernate and Standby

源代码网整理以下To put the system in hibernate and standby modes, we are going to use Application class"s SetSuspendState method. There are three arguments for this function, power state, force and disable wake event. The first argument, power state is where we mention the state of the system (hibernate/suspend)

源代码网整理以下// Hibernate
Application.SetSuspendState(PowerState.Hibernate, true, true);
// Standby
Application.SetSuspendState(PowerState.Suspend true, true);

源代码网整理以下Conclusion 软件开发网 www.mscto.com

源代码网整理以下Please note that these methods were only tested in Windows XP Home and Professional environments, however they should work on any systems that have .NET Framework installed.


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华