C#实现关机的两种方法

您所在的位置:网站首页 关闭电脑自动关机命令 C#实现关机的两种方法

C#实现关机的两种方法

2023-09-12 22:50| 来源: 网络整理| 查看: 265

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来实现 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace TestShutdown { class SystemUtil { [StructLayout(LayoutKind.Sequential, Pack = 1)] internal struct TokPriv1Luid { public int Count; public long Luid; public int Attr; } [DllImport("kernel32.dll", ExactSpelling = true)] internal static extern IntPtr GetCurrentProcess(); [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok); [DllImport("advapi32.dll", SetLastError = true)] internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid); [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen); [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] internal static extern bool ExitWindowsEx(int flg, int rea); internal const int SE_PRIVILEGE_ENABLED = 0x00000002; internal const int TOKEN_QUERY = 0x00000008; internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; internal const int EWX_LOGOFF = 0x00000000; internal const int EWX_SHUTDOWN = 0x00000001; internal const int EWX_REBOOT = 0x00000002; internal const int EWX_FORCE = 0x00000004; internal const int EWX_POWEROFF = 0x00000008; internal const int EWX_FORCEIFHUNG = 0x00000010; private static void DoExitWin(int flg) { bool ok; TokPriv1Luid tp; IntPtr hproc = GetCurrentProcess(); IntPtr htok = IntPtr.Zero; ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); tp.Count = 1; tp.Luid = 0; tp.Attr = SE_PRIVILEGE_ENABLED; ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid); ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); ok = ExitWindowsEx(flg, 0); } public static void Reboot() { DoExitWin(EWX_FORCE | EWX_REBOOT); //重启 } public static void PowerOff() { DoExitWin(EWX_FORCE | EWX_POWEROFF); //关机 } public static void LogoOff() { DoExitWin(EWX_FORCE | EWX_LOGOFF); //注销 } } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3