锁定计算机 注销 休眠,C#下计算机锁定, 注销, 重启, 关机, 休眠, 待机

【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.

0818b9ca8b590ca3270a3433284dd417.png

【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.

usingSystem.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 intExitWindowsEx(int uFlags, int dwReason);Double click the log off button and add the following function call to the event. Flag 0 indicates logoff,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】

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.

Thats all folks!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值