C#实现控制Windows系统关机、重启和注销的方法

shutdown.exe -s:关机
shutdown.exe -r:关机并重启
shutdown.exe -l:注销当前用户

shutdown.exe -s -t 时间:设置关机倒计时
shutdown.exe -h:休眠
shutdown.exe -t 时间:设置关机倒计时。默认值是 30 秒。
shutdown.exe -a:取消关机
shutdown.exe -f:强行关闭应用程序而没有警告
shutdown.exe -m \计算机名:控制远程计算机
shutdown.exe -i:显示“远程关机”图形用户界面,但必须是Shutdown的第一个参数
shutdown.exe -c "消息内容":输入关机对话框中的消息内容
shutdown.exe -d [u][p]:xx:yy :列出系统关闭的原因代码:u 是用户代码 ,p 是一个计划的关闭代码 ,xx 是一个主要原因代码(小于 256 的正整数) ,yy 是一个次要原因代码(小于 65536 的正整数)

比如你的电脑要在12:00关机,可以选择“开始→运行”,输入“at 12:00 Shutdown -s",这样,到了12点电脑就会出现“系统关机”对话框,默认有30秒钟的倒计时并提示你保存工作。
如果你想以倒计时的方式关机,可以输入 “Shutdown.exe -s -t 3600",这里表示60分钟后自动关机,“3600"代表60分钟。
一键关机:
1、首先在桌面的空白处单击鼠标右键,新建一个“快捷方式”。
2、在创建快捷方式的“命令行”中输入以下的指令:
“shutdown –s –t 0 ”。(在windows98按此输入“C:windowsRUNDLL32.EXE user,ExitWindows”。)
3、按着鼠标选择“下一步”,在快捷方式的名称栏中输入“一键关机”或其他自己喜欢的名称。
4、之后,你就会在桌面见到一个名为“一键关机”的快捷方式图标,在该图标上单击鼠标右键,选择“属性”,再进入“快捷方式”页,然后在“快速键一栏内随便按选一个功能键(如F1-F12)。建议大家最好选一个平时不常用的功能键,最后按确定退出即可。

Windows系统通过一个名为shutdown.exe的程序来完成关机操作(位置Windows\System32下),一般情况下Windows系统的关机都可以由关机程序 shutdown.exe来实现的,关机的时候调用shutdown.exe。由此可知要阻止强行关机就是要取消对shutdown.exe的调用。

使用C#代码实现控制Windows系统关机、重启和注销的方法,使用.NET和C#.NET,我们可以对当前PC执行关机,重启,注销操作,
.NET Framework中,有一个命名空间System.Diagnostics具有所需的类和方法,从当前PC上运行.NET应用程序来执行这些操作 。一般使用System.Diagnostics.Process.Start()方法来启动shutdown.exe程序。
下面是一个winform程序说明,使用按钮来执行关机,重启和注销。

//关机 和 计时关机
private void btnShutDown_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
if (MessageBox.Show("将要设定计划关机,是否确认操作?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
decimal decl = numericUpDown1.Value * 3600 + numericUpDown2.Value * 60 + numericUpDown3.Value;
string str = decl.ToString();
Process.Start("shutdown.exe", "-s -t " + str);//计时关机
}
}
else
{
if (MessageBox.Show("是否确认关机?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Process.Start("shutdown.exe", "-s");//关机
//Process.Start("shutdown.exe", "-s -t xx");
}
}
}
//重启
private void butRestar_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否确认重启?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Process.Start("shutdown.exe", "-r");//重启
Process.Start("shutdown.exe", "-r -t 10");
}
}
//注销
private void butLogOff_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否确认注销?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
Process.Start("shutdown.exe", "-l");//注销
}

原文链接:https://www.cnblogs.com/xifengyeluo/p/5914883.html

 1         //关机 和 计时关机
 2         private void btnShutDown_Click(object sender, EventArgs e)
 3         {
 4             if (checkBox1.Checked)
 5             {
 6                 if (MessageBox.Show("将要设定计划关机,是否确认操作?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
 7                 {
 8                     decimal decl = numericUpDown1.Value * 3600 + numericUpDown2.Value * 60 + numericUpDown3.Value;
 9                     string str = decl.ToString();
10                     Process.Start("shutdown.exe", "-s -t " + str);//计时关机
11                 }
12             }
13             else
14             {
15                 if (MessageBox.Show("是否确认关机?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
16                 {
17                     Process.Start("shutdown.exe", "-s");//关机
18                     //Process.Start("shutdown.exe", "-s -t xx");
19                 }
20             }
21         }
22         //重启
23         private void butRestar_Click(object sender, EventArgs e)
24         {
25             if (MessageBox.Show("是否确认重启?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
26             {
27                 Process.Start("shutdown.exe", "-r");//重启
28                 Process.Start("shutdown.exe", "-r -t 10");
29             }
30         }
31         //注销
32         private void butLogOff_Click(object sender, EventArgs e)
33         {
34             if (MessageBox.Show("是否确认注销?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
35                 Process.Start("shutdown.exe", "-l");//注销
36         }
37
38
39
40
41
42  

原文地址:https://www.cnblogs.com/1175429393wljblog/p/9001792.html

时间: 2024-08-07 04:32:15

C#实现控制Windows系统关机、重启和注销的方法的相关文章

C#实现控制Windows系统关机、重启和注销的方法:

shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设置关机倒计时shutdown.exe -h:休眠shutdown.exe -t 时间:设置关机倒计时.默认值是 30 秒.shutdown.exe -a:取消关机shutdown.exe -f:强行关闭应用程序而没有警告shutdown.exe -m \计算机名:控制远程计算机shutdown.ex

C#控制Windows系统关机、重启和注销的代码

如下资料是关于C#控制Windows系统关机.重启和注销的代码. protected void btnShutDown_Click(object sender, EventArgs e) { } protected void btnRestart_Click(object sender, EventArgs e) { } protected void btnLogOff_Click(object sender, EventArgs e) { } 原文地址:https://www.cnblogs.

Linux 系统关机重启命令

Linux 系统关机重启 关机 (系统的关机.重启以及登出 ) shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours:minutes & 按预定时间关闭系统 shutdown -c 取消按预定时间关闭系统 shutdown -r now 重启(1) reboot 重启(2) logout 注销 原文地址:https://www.cnblogs.com/huao990928/p/12321815.htm

Windows系统下Python与NumPy安装方法

Windows系统下Python与NumPy安装方法 Windows下Python的某些第三方包安装步骤实在是太麻烦了(这里主要以NumPy为例,目前只有遇到安装它的时候出现了很多问题),晚上花了好几个小时才把NumPy科学计算包安装好,在这里描述下安装过程,避免大家走没有必要的弯路. 1,安装Python 首先,运行下载的MSI安装包,选择安装组件时,确保勾上了所有的组件. 特别要注意选上pip和Add python.exe to Path,然后多次点击Next即可完成安装. Python解释

Windows 系统关机、重启、睡眠、休眠及唤醒消息

今天要查找如何获取系统从睡眠.休眠状态下唤醒的消息,写了个MFC对话框的程序,贴出部分核心代码: //唤醒消息捕获 LRESULT CSystemResumedMessageDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { // TODO: 在此添加专用代码和/或调用基类 if ((message == WM_POWERBROADCAST) && (wParam == PBT_APMRESUMEAUTOMATIC))

windows远程关机重启

windows远程关机 http://lsscto.blog.51cto.com/779396/245681 shutdown http://baike.baidu.com/view/596875.htm 用法: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "comment"] [-d up:xx:yy]              没有参数                     显示此消

windows 系统如何开启“上帝模式”的方法

上帝模式,即"God Mode",是Windows 7系统中隐藏的一个简单的文件夹窗口,但包含了几乎所有Windows 7系统的设置,如控制面板的功能.界面个性化.辅助功能选项等方方面面的控制设置,用户只需通过这一个窗口就能实现所有的操控,而不必再去为调整一个小小的系统设置细想半天究竟该在什么地方去打开设置窗口.此功能同样适用于Windows 8操作系统. Windows 系统(包括windows7.windows8.windows 8.1.windows 10等)的"God

ansible控制windows安装及运行error与解决方法

Q1:安装kerberos报错 $ sudo pip install kerberos running build_ext building 'kerberos' extension creating build creating build/temp.linux-x86_64-2.7 creating build/temp.linux-x86_64-2.7/src gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -

在 Windows 系统下安装 IntelliJ IDEA 的方法

首先,进入官网下载 IntelliJ IDEA 可执行文件:http://www.dongguanqingjiegongsi.cn/ 1 如上图所示,进入"Download IntelliJ IDEA"页面,显然咱们可以看到 IntelliJ IDEA 分为两个版本,分别为: 旗舰版(Ultimate)社区版(Community)其中,旗舰版是收费的,社区版则是免费的.至于两者的区别嘛,就是旗舰版比社区版的功能更为齐全!如果你想用 IntelliJ IDEA 进行大型项目开发的的话,啥