使用c#对windows进行关机、重启或注销
方法一:调用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
- private const int SE_PRIVILEGE_ENABLED = 0x00000002;
- private const int TOKEN_QUERY = 0x00000008;
- private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
- private const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
- [Flags]
- public enum ExitWindows : uint
- {
- LogOff = 0x00, //注销
- ShutDown = 0x01, //关机
- Reboot = 0x02, //重启
- Force = 0x04,
- PowerOff = 0x08,
- ForceIfHung = 0x10
- }
- [Flags]
- private enum ShutdownReason : uint
- {
- MajorApplication = 0x00040000,
- MajorHardware = 0x00010000,
- MajorLegacyApi = 0x00070000,
- MajorOperatingSystem = 0x00020000,
- MajorOther = 0x00000000,
- MajorPower = 0x00060000,
- MajorSoftware = 0x00030000,
- MajorSystem = 0x00050000,
- MinorBlueScreen = 0x0000000F,
- MinorCordUnplugged = 0x0000000b,
- MinorDisk = 0x00000007,
- MinorEnvironment = 0x0000000c,
- MinorHardwareDriver = 0x0000000d,
- MinorHotfix = 0x00000011,
- MinorHung = 0x00000005,
- MinorInstallation = 0x00000002,
- MinorMaintenance = 0x00000001,
- MinorMMC = 0x00000019,
- MinorNetworkConnectivity = 0x00000014,
- MinorNetworkCard = 0x00000009,
- MinorOther = 0x00000000,
- MinorOtherDriver = 0x0000000e,
- MinorPowerSupply = 0x0000000a,
- MinorProcessor = 0x00000008,
- MinorReconfig = 0x00000004,
- MinorSecurity = 0x00000013,
- MinorSecurityFix = 0x00000012,
- MinorSecurityFixUninstall = 0x00000018,
- MinorServicePack = 0x00000010,
- MinorServicePackUninstall = 0x00000016,
- MinorTermSrv = 0x00000020,
- MinorUnstable = 0x00000006,
- MinorUpgrade = 0x00000003,
- MinorWMI = 0x00000015,
- FlagUserDefined = 0x40000000,
- FlagPlanned = 0x80000000
- }
- [StructLayout(LayoutKind.Sequential, Pack = )]
- private struct TokPriv1Luid
- {
- public int Count;
- public long Luid;
- public int Attr;
- }
- [DllImport("kernel32.dll", ExactSpelling = true)]
- private static extern IntPtr GetCurrentProcess();
- [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
- private static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
- [DllImport("advapi32.dll", SetLastError = true)]
- private static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
- [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
- private static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
- [DllImport("user32.dll")]
- private static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
- /// <summary>
- /// 关机、重启、注销windows
- /// </summary>
- /// <param name="flag"></param>
- public static void DoExitWindows(ExitWindows flag)
- {
- TokPriv1Luid tp;
- IntPtr hproc = GetCurrentProcess();
- IntPtr htok = IntPtr.Zero;
- OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
- tp.Count = ;
- tp.Luid = ;
- tp.Attr = SE_PRIVILEGE_ENABLED;
- LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
- AdjustTokenPrivileges(htok, false, ref tp, , IntPtr.Zero, IntPtr.Zero);
- ExitWindowsEx(flag, ShutdownReason.MajorOther);
- }
http://www.cnblogs.com/sosoft/p/3459826.html
使用c#对windows进行关机、重启或注销的更多相关文章
- windows远程关机重启
windows远程关机 http://lsscto.blog.51cto.com/779396/245681 shutdown http://baike.baidu.com/view/596875.h ...
- windows 上驱动阻止关机重启操作
Windows 上关机重启有很多相关的操作 HOOK 一个点搞不定 具体需要以下 4 处来布控 SSDT HOOK NtInitiatePowerAction 函数 ,直接返回失败废掉这个函数 SS ...
- 第五章 Linux操作系统关机、重启、注销及其查看ip命令
一.更新系统时间与网络时间同步 1. 安装ntpdate工具 # yum -y install ntp ntpdate 2. 设置系统时间与网络时间同步 # ntpdate cn.pool.ntp ...
- C#实现控制Windows系统关机、重启和注销的方法:
shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设 ...
- 编程实现Windows关机、重启、注销
要想编程使Windows关机.重启或者注销,可以使用ExWindowsEx这个API函数,该函数只有两个参数,第一个表示关机动作的标志,也就是你要让该函数关机呢,还是重启,还是注销等.可以使用EWX_ ...
- C#实现控制Windows系统关机、重启和注销的方法
shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设 ...
- windows 关机 重启 命令
关机 shutdown -s -t 0 重启 shutdown -r -t 0 重启电脑windows 立即关机 shutdown -s 三十分钟后关机 shutdown -s -t 30 举例说明一 ...
- 在Closing事件中,将e.Cancle设置成true,则Windows无法关机和重启系统的解决办法
最近在设计一个WinForm程序的时候遇到一个bug,就是From1窗体的关闭事件中设置了e.Cancle设置成true,导致系统无法关机重启,windows7 和windows xp都是这样. 我这 ...
- Windows 8关机的三个最简单方法
Win8怎么关机?全新的Win8系统给用户一个难题,Win8如何关机?笔者整理了Win8关机的最实用的三个方法,希望能让大家了解Win8关机的具体操作,解决Win8如何关机等问题. 最常规的Win8关 ...
随机推荐
- 培训SQLServer 嵌套事务PPT分享
培训SQLServer 嵌套事务PPT分享 下载地址 http://files.cnblogs.com/files/lyhabc/SQLServer%E5%B5%8C%E5%A5%97%E4%BA%8 ...
- BrnShop mvc3升级mvc4
此文来自:http://www.cnblogs.com/fumj/p/3588517.html 手工升级ASP.NET MVC 3项目: 一.安装ASP.NET MVC 4 二.升级ASP.NET M ...
- [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序
[.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序 本节导读: 本节主要说明使用异步进行程序设计的优缺点及如何通过异步编程. 使用 ...
- upload4j安全、高效、易用的java http文件上传框架
简介 upload4j是一款轻量级http文件上传框架,使用简单,实现高效,功能专一,摆脱传统http文件上传框架的繁琐. upload4j的诞生并不是为了解决所有上传需求,而是专注于基础通用需求. ...
- jdbc在mysql下一次执行多条sql脚本
默认连接mysql的时候一次只能执行一条sql.要批量执行sql需要在jdbcUrl中增加“allowMultiQueries=true”参数,完整jdbcUrl如下: jdbc:mysql://l ...
- Linux查找命令:grep,awk,sed
grep grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具 ...
- Jetty Maven Plugin配置
官方文档:http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#maven-config-https 1 ...
- Security9:查询Login被授予的权限
在给一个Login授予权限时,发现该Login已经存在,其对应的User也存在于指定的DB中,查看该Login在指定DB中已被授予的权限. 1,查看Login的Server PrincipalID s ...
- 打造自己的html5视频播放器
前段时间重新学习了一下html5的video部分,以前只是停留在标签的使用上,这一次决定深入了解相关的API,并运用这些API打造一个简单的视频播放器.所谓“打造自己的”,就是要自己重写video标签 ...
- AngularJS API之$injector ---- 依赖注入
在AngularJS中也有依赖注入的概念,像spring中的依赖注入,但是又有所不同.Spring中使用构造注入或者设值注入的方式,还需要做一些额外的操作,但是angular中只需要在需要的地方声明一 ...