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 }

 

 

posted @ 2012-07-13 22:58  我爱班花  阅读(6567)  评论(0编辑  收藏  举报