方法一:调用windows自带的shutdown.exe (缺点:会出现倒计时窗口)

System.Diagnostics.Process.Start("shutdown.exe", "-r -f -t 15");

shutdown参数含义:-r关闭并重启动此计算机;-f 强制运行的应用程序关闭而没有警告;-t 15 延时15秒
shutdown.exe的详细用法:
shutdown [-i | -l | -s | -r | -a] [-f] [-m //computername] [-t xx] [-c "comment"] [-d up:xx:yy]
没有参数 显示此消息(与 ? 相同)
-i 显示 GUI 界面,必须是第一个选项
-l 注销(不能与选项 -m 一起使用)
-s 关闭此计算机
-r 关闭并重启动此计算机
-a 放弃系统关机
-m //computername 远程计算机关机/重启动/放弃
-t xx 设置关闭的超时为 xx 秒
-c "comment" 关闭注释(最大 127 个字符)
-f 强制运行的应用程序关闭而没有警告
-d [u][p]:xx:yy 关闭原因代码
u 是用户代码
p 是一个计划的关闭代码
xx 是一个主要原因代码(小于 256 的正整数)
yy 是一个次要原因代码(小于 65536 的正整数)

方法二:调用API

  1. private const int SE_PRIVILEGE_ENABLED = 0x00000002;
  2. private const int TOKEN_QUERY = 0x00000008;
  3. private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  4. private const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
  5.  
  6. [Flags]
  7. public enum ExitWindows : uint
  8. {
  9. LogOff = 0x00, //注销
  10. ShutDown = 0x01, //关机
  11. Reboot = 0x02, //重启
  12. Force = 0x04,
  13. PowerOff = 0x08,
  14. ForceIfHung = 0x10
  15. }
  16.  
  17. [Flags]
  18. private enum ShutdownReason : uint
  19. {
  20. MajorApplication = 0x00040000,
  21. MajorHardware = 0x00010000,
  22. MajorLegacyApi = 0x00070000,
  23. MajorOperatingSystem = 0x00020000,
  24. MajorOther = 0x00000000,
  25. MajorPower = 0x00060000,
  26. MajorSoftware = 0x00030000,
  27. MajorSystem = 0x00050000,
  28. MinorBlueScreen = 0x0000000F,
  29. MinorCordUnplugged = 0x0000000b,
  30. MinorDisk = 0x00000007,
  31. MinorEnvironment = 0x0000000c,
  32. MinorHardwareDriver = 0x0000000d,
  33. MinorHotfix = 0x00000011,
  34. MinorHung = 0x00000005,
  35. MinorInstallation = 0x00000002,
  36. MinorMaintenance = 0x00000001,
  37. MinorMMC = 0x00000019,
  38. MinorNetworkConnectivity = 0x00000014,
  39. MinorNetworkCard = 0x00000009,
  40. MinorOther = 0x00000000,
  41. MinorOtherDriver = 0x0000000e,
  42. MinorPowerSupply = 0x0000000a,
  43. MinorProcessor = 0x00000008,
  44. MinorReconfig = 0x00000004,
  45. MinorSecurity = 0x00000013,
  46. MinorSecurityFix = 0x00000012,
  47. MinorSecurityFixUninstall = 0x00000018,
  48. MinorServicePack = 0x00000010,
  49. MinorServicePackUninstall = 0x00000016,
  50. MinorTermSrv = 0x00000020,
  51. MinorUnstable = 0x00000006,
  52. MinorUpgrade = 0x00000003,
  53. MinorWMI = 0x00000015,
  54. FlagUserDefined = 0x40000000,
  55. FlagPlanned = 0x80000000
  56. }
  57.  
  58. [StructLayout(LayoutKind.Sequential, Pack = )]
  59. private struct TokPriv1Luid
  60. {
  61. public int Count;
  62. public long Luid;
  63. public int Attr;
  64. }
  65.  
  66. [DllImport("kernel32.dll", ExactSpelling = true)]
  67. private static extern IntPtr GetCurrentProcess();
  68.  
  69. [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  70. private static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
  71.  
  72. [DllImport("advapi32.dll", SetLastError = true)]
  73. private static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
  74.  
  75. [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  76. private static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
  77.  
  78. [DllImport("user32.dll")]
  79. private static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
  80.  
  81. /// <summary>
  82. /// 关机、重启、注销windows
  83. /// </summary>
  84. /// <param name="flag"></param>
  85. public static void DoExitWindows(ExitWindows flag)
  86. {
  87. TokPriv1Luid tp;
  88. IntPtr hproc = GetCurrentProcess();
  89. IntPtr htok = IntPtr.Zero;
  90. OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
  91. tp.Count = ;
  92. tp.Luid = ;
  93. tp.Attr = SE_PRIVILEGE_ENABLED;
  94. LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
  95. AdjustTokenPrivileges(htok, false, ref tp, , IntPtr.Zero, IntPtr.Zero);
  96. ExitWindowsEx(flag, ShutdownReason.MajorOther);
  97. }

http://www.cnblogs.com/sosoft/p/3459826.html

使用c#对windows进行关机、重启或注销的更多相关文章

  1. windows远程关机重启

    windows远程关机 http://lsscto.blog.51cto.com/779396/245681 shutdown http://baike.baidu.com/view/596875.h ...

  2. windows 上驱动阻止关机重启操作

    Windows 上关机重启有很多相关的操作 HOOK 一个点搞不定  具体需要以下 4 处来布控 SSDT HOOK NtInitiatePowerAction 函数 ,直接返回失败废掉这个函数 SS ...

  3. 第五章 Linux操作系统关机、重启、注销及其查看ip命令

    一.更新系统时间与网络时间同步 1.  安装ntpdate工具 # yum -y install ntp ntpdate 2.  设置系统时间与网络时间同步 # ntpdate cn.pool.ntp ...

  4. C#实现控制Windows系统关机、重启和注销的方法:

    shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设 ...

  5. 编程实现Windows关机、重启、注销

    要想编程使Windows关机.重启或者注销,可以使用ExWindowsEx这个API函数,该函数只有两个参数,第一个表示关机动作的标志,也就是你要让该函数关机呢,还是重启,还是注销等.可以使用EWX_ ...

  6. C#实现控制Windows系统关机、重启和注销的方法

    shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设 ...

  7. windows 关机 重启 命令

    关机 shutdown -s -t 0 重启 shutdown -r -t 0 重启电脑windows 立即关机 shutdown -s 三十分钟后关机 shutdown -s -t 30 举例说明一 ...

  8. 在Closing事件中,将e.Cancle设置成true,则Windows无法关机和重启系统的解决办法

    最近在设计一个WinForm程序的时候遇到一个bug,就是From1窗体的关闭事件中设置了e.Cancle设置成true,导致系统无法关机重启,windows7 和windows xp都是这样. 我这 ...

  9. Windows 8关机的三个最简单方法

    Win8怎么关机?全新的Win8系统给用户一个难题,Win8如何关机?笔者整理了Win8关机的最实用的三个方法,希望能让大家了解Win8关机的具体操作,解决Win8如何关机等问题. 最常规的Win8关 ...

随机推荐

  1. 培训SQLServer 嵌套事务PPT分享

    培训SQLServer 嵌套事务PPT分享 下载地址 http://files.cnblogs.com/files/lyhabc/SQLServer%E5%B5%8C%E5%A5%97%E4%BA%8 ...

  2. BrnShop mvc3升级mvc4

    此文来自:http://www.cnblogs.com/fumj/p/3588517.html 手工升级ASP.NET MVC 3项目: 一.安装ASP.NET MVC 4 二.升级ASP.NET M ...

  3. [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序

    [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序 本节导读: 本节主要说明使用异步进行程序设计的优缺点及如何通过异步编程. 使用 ...

  4. upload4j安全、高效、易用的java http文件上传框架

    简介 upload4j是一款轻量级http文件上传框架,使用简单,实现高效,功能专一,摆脱传统http文件上传框架的繁琐. upload4j的诞生并不是为了解决所有上传需求,而是专注于基础通用需求. ...

  5. jdbc在mysql下一次执行多条sql脚本

    默认连接mysql的时候一次只能执行一条sql.要批量执行sql需要在jdbcUrl中增加“allowMultiQueries=true”参数,完整jdbcUrl如下:  jdbc:mysql://l ...

  6. Linux查找命令:grep,awk,sed

    grep grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具 ...

  7. Jetty Maven Plugin配置

    官方文档:http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#maven-config-https 1 ...

  8. Security9:查询Login被授予的权限

    在给一个Login授予权限时,发现该Login已经存在,其对应的User也存在于指定的DB中,查看该Login在指定DB中已被授予的权限. 1,查看Login的Server PrincipalID s ...

  9. 打造自己的html5视频播放器

    前段时间重新学习了一下html5的video部分,以前只是停留在标签的使用上,这一次决定深入了解相关的API,并运用这些API打造一个简单的视频播放器.所谓“打造自己的”,就是要自己重写video标签 ...

  10. AngularJS API之$injector ---- 依赖注入

    在AngularJS中也有依赖注入的概念,像spring中的依赖注入,但是又有所不同.Spring中使用构造注入或者设值注入的方式,还需要做一些额外的操作,但是angular中只需要在需要的地方声明一 ...