C#实现关机的两种⽅法
1、使⽤shutdown关机命令来实现。
using System.Diagnostics;
int time = 3600; //单位为:秒
Process.Start("c:/windows/", "-s -t "+time);
ShutDown⽤法及参数(XP)
⽤法: 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 的正整数)
-
f:强⾏关闭应⽤程序
-m 计算机名:控制远程计算机
-i:显⽰图形⽤户界⾯,但必须是Shutdown的第⼀个选项
-l:注销当前⽤户
-r:关机并重启
-t时间:设置关机倒计时
-c "消息内容":输⼊关机对话框中的消息内容(不能超127个字符)
具体使⽤⽅法可参考的命令⾏指令。这种⽅法可在PC上使⽤,不过当系统为WINCE时,WINCE没有,所以该⽅法将不再使⽤。可⽤第⼆种⽅法。
2、调⽤WIN32 API来实现
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Runtime.InteropServices;
5
6namespace TestShutdown
7 {
8class SystemUtil
9 {
10 [StructLayout(LayoutKind.Sequential, Pack = 1)]
11internal struct TokPriv1Luid
12 {
13public int Count;
14public long Luid;
15public int Attr;
16 }
17
18 [DllImport("kernel32.dll", ExactSpelling = true)]
19internal static extern IntPtr GetCurrentProcess();
20
21 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
22internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
23
24 [DllImport("advapi32.dll", SetLastError = true)]
25internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
26
27 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
28internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen); 29
30 [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
31internal static extern bool ExitWindowsEx(int flg, int rea);
32
33internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
34internal const int TOKEN_QUERY = 0x00000008;
35internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
36internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
37internal const int EWX_LOGOFF = 0x00000000;
38internal const int EWX_SHUTDOWN = 0x00000001;
39internal const int EWX_REBOOT = 0x00000002;
40internal const int EWX_FORCE = 0x00000004;
41internal const int EWX_POWEROFF = 0x00000008;
42internal const int EWX_FORCEIFHUNG = 0x00000010;
43
44private static void DoExitWin(int flg)
45 {
46bool ok;
47 TokPriv1Luid tp;
48 IntPtr hproc = GetCurrentProcess();
49 IntPtr htok = IntPtr.Zero;
50 ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
51 tp.Count = 1;
关机程序代码52 tp.Luid = 0;
53 tp.Attr = SE_PRIVILEGE_ENABLED;
54 ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
55 ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
56 ok = ExitWindowsEx(flg, 0);
57 }
58
59public static void Reboot()
60 {
61 DoExitWin(EWX_FORCE | EWX_REBOOT); //重启
62 }
63
64public static void PowerOff()
65 {
66 DoExitWin(EWX_FORCE | EWX_POWEROFF); //关机
67 }
68
69public static void LogoOff()
70 {
71 DoExitWin(EWX_FORCE | EWX_LOGOFF); //注销
72 }
73
74 }
75
76 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论