C#实现关机的两种方法

  • 格式:pdf
  • 大小:59.55 KB
  • 文档页数:2

C#实现关机的两种⽅法

1、使⽤shutdown关机命令来实现。

using System.Diagnostics;

 int time = 3600; //单位为:秒

 Process.Start("c:/windows/system32/shutdown.exe", "-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个字符)

具体使⽤⽅法可参考shutdown.exe的命令⾏指令。这种⽅法可在PC上使⽤,不过当系统为WINCE时,WINCE没有shutdown.exe,所

以该⽅法将不再使⽤。可⽤第⼆种⽅法。

2、调⽤WIN32 API来实现

1 using System;

2 using System.Collections.Generic;

3 using System.Text;

4 using System.Runtime.InteropServices;

5

6 namespace TestShutdown

7 {

8 class SystemUtil

9 {

10 [StructLayout(LayoutKind.Sequential, Pack = 1)]

11 internal struct TokPriv1Luid

12 {

13 public int Count;14 public long Luid;

15 public int Attr;

16 }

17

18 [DllImport("kernel32.dll", ExactSpelling = true)]

19 internal static extern IntPtr GetCurrentProcess();

20

21 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

22 internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

23

24 [DllImport("advapi32.dll", SetLastError = true)]

25 internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

26

27 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

28 internal 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)]

31 internal static extern bool ExitWindowsEx(int flg, int rea);

32

33 internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

34 internal const int TOKEN_QUERY = 0x00000008;

35 internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

36 internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

37 internal const int EWX_LOGOFF = 0x00000000;

38 internal const int EWX_SHUTDOWN = 0x00000001;

39 internal const int EWX_REBOOT = 0x00000002;

40 internal const int EWX_FORCE = 0x00000004;

41 internal const int EWX_POWEROFF = 0x00000008;

42 internal const int EWX_FORCEIFHUNG = 0x00000010;

43

44 private static void DoExitWin(int flg)

45 {

46 bool 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

59 public static void Reboot()

60 {

61 DoExitWin(EWX_FORCE | EWX_REBOOT); //重启

62 }

63

64 public static void PowerOff()

65 {

66 DoExitWin(EWX_FORCE | EWX_POWEROFF); //关机

67 }

68

69 public static void LogoOff()

70 {

71 DoExitWin(EWX_FORCE | EWX_LOGOFF); //注销

72 }

73

74 }

75

76 }