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

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!

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

  1. 班级文化建设计算机专业,云计算背景下计算机专业三二段学生班级文化建设研究①...

    但唐仁,何涛,覃国蓉,李华忠,杨海红 [摘 要] 通过调查问卷.学生访谈.教师反馈等形式,对云计算背景下计算机专业三二段学生班级文化建设这一问题进行了研究.深入浅出地分析了云计算背景下计算机专业三二段 ...

  2. 计算机一级试题选择题下,计算机一级考试试题选择题(完整)(0)

    福建省计算机一级考试试题选择题(完整)(0) 计算机等级考试练习题(选择题) 第一章 第二章习题 1. 电报是属于第( )次信息技术革命的产物. A. 三 B. 五 C. 四 D. 二 2. 下列叙述 ...

  3. 锁定计算机后怎样解锁,如何在锁定键盘时解锁计算机键盘. 详细的方法介绍

    每个人都应该了解计算机!计算机在我们的日常生活中起着重要的作用. 我们通常需要用于购物和娱乐的计算机. 不仅如此,现在计算机还广泛用于军事和政治领域,而且日常生活中的计算机是每个人通常都能使用的计算机 ...

  4. 华硕关机前显示正在锁定计算机是什么意思,将桌面锁定在启动状态的快捷键是什么?如何锁定联想笔记本电脑的屏幕?什么是快捷键?...

    相关问题 将桌面锁定在启动状态的快捷键是什么?如何一键锁定屏幕Windows10锁定计算机的快捷键? 在控制面板中设置您自己的登录密码,按住键盘的开始按钮(通常是左下角的第二个按钮,即开始图标的按钮) ...

  5. win7设置开机后自动锁定计算机,Win7系统锁定计算机怎么设置 win7系统自动锁定...

    原教程:"Win7系统锁定计算机怎么设置?"的相关最新电脑教程资料分享. - 来源:191路由网 - 编辑:阿狸. 不知道大家有没有遇到这样的情况,长时间不操作计算机的话,计算机就 ...

  6. win7设置定时锁定计算机,Win7系统锁定计算机怎么设置?Win7系统锁定计算机的设置方法...

    相信大家都知道,长时间不操作计算机,计算机就会自动进入锁定状态,不过,最近有Win7系统用户表示不想让计算机自动进入锁定状态.那么,Win7系统锁定计算机怎么设置呢?下面,我们就一起往下看看Win7系 ...

  7. 键盘怎么锁定计算机,如何解锁键盘?键盘锁定了怎么办?

    对于办公室工作人员来说,计算机绝对是我们工作的好帮手. 我们需要每天使用它来接收电子邮件和组织工作文档. 由于上班族的工作非常依赖于计算机,因此我通常都会基本了解一些计算机维修技能,但是对于许多办公室 ...

  8. 电脑锁定计算机怎么办,技巧:如果显示器菜单被锁定,该怎么办_计算机的基本知识_IT /计算机_信息...

    本文在Internet上组织,仅供阅读.如果显示菜单被锁定怎么办?解决方案:1.有时,计算机上也会出现类似的提示.2.这是因为计算机制造商添加了屏幕锁以防止屏幕误操作.出现此框.您必须不小心单击了屏幕 ...

  9. win7锁定计算机任务栏,Windows7任务栏锁定功能怎么使用?win7使用任务栏锁定功能的方法...

    电脑升级到Windows7系统后任务栏功能也有所增强了,其中任务栏"锁定"功能非常实用.除了锁定程序图标外,任务栏锁定功能还有其他的用途.那么Windows7怎么使用任务栏锁定功能 ...

最新文章

  1. GitHub上7000+ Star的Python常用代码合集
  2. 图片验证码识别 python web_Python web中实现验证码图片
  3. Eular 函数模板
  4. NYOJ 30 Gone Fishing(贪心)
  5. batchplot插件用法_Batchplot批量打印怎么用?Batchplot批量打印教程
  6. java激励_激励干个人java的不足之处
  7. java 实现中文排序,Java自定义比较器实现中文排序
  8. webUI自动化一元素定位
  9. python表白小程序(1)源码下载
  10. 计算机应用基础课改期望,《计算机应用基础》课改总结.doc
  11. 基于PHP以及JQUERY AJAX的计算器实例
  12. php安装完后 apache the requested has,Apache启动报错:the requested operation has failed
  13. PyTorch-GPU版本、Tensorflow-GPU版本配置
  14. 【BZOJ】3524: [Poi2014]Couriers
  15. 计算机用户里的AppData,AppData是什么意思,Appdata文件夹可以删除吗?
  16. pmp知识点(9)-项目资源管理
  17. BGP是做什么的?怎么去配置BGP?
  18. 诊断DB2 Java应用程序的性能问题
  19. SAP打开财务和物料的账期(OB52 MMRV MMPV)
  20. 安卓Camera一代基于虹软人脸识别程序开发

热门文章

  1. 【转】python入门教程
  2. Tensorflow使用Char-CNN实现中文文本分类(1)
  3. mysql nullpoint_访问数据库时遇到NullPointer Exception
  4. android retrofit 2.0公共参数,Android Retrofit 2.0 使用-补充篇
  5. 如何在日语键盘布局上使用正确的按键顺序
  6. 1087 All Roads Lead to Rome (30 分)
  7. 车联网算法之概述(一)
  8. 如何解决问题恢复现状?
  9. 考研机试:数学问题之日期类问题
  10. go os.Args 学习