用c语言编写的关机程序
c#实现关机,注销,重启源代码

Re:c#实现关机,注销,重启源代码另一份代码using System;using System.Drawing;using System.Collections;using ponentModel;using System.Windows.Forms;using System.Data;using System.Management;using System.Runtime.InteropServices; //加入此名字间是为了引用Windows API来实现关机namespace RebootComputer{public class Form1 : System.Windows.Forms.Form{//---------------这部分都是控件--------private System.Windows.Forms.Button Reboot;private System.Windows.Forms.Button ShutDown;private ponentModel.Container components = null;private System.Windows.Forms.Button Exit;//---------------------------------------------------------private ManagementObjectSearcher obj ;//-----------------程序初始化----------------public Form1(){InitializeComponent();}//-----------------------程序释放----------------------protected override void Dispose( bool disposing ){if( disposing ){if (components != null){components.Dispose();}}base.Dispose( disposing );}//-------------------------初始化----------------------private void InitializeComponent(){this.Reboot = new System.Windows.Forms.Button();this.ShutDown = new System.Windows.Forms.Button();this.Exit = new System.Windows.Forms.Button();this.SuspendLayout();//// Reboot//this.Reboot.Location = new System.Drawing.Point(16, 16); = "Reboot";this.Reboot.TabIndex = 0;this.Reboot.Text = "重启";this.Reboot.Click += new System.EventHandler(this.Reboot_Click);//// ShutDown//this.ShutDown.Location = new System.Drawing.Point(112, 16); = "ShutDown";this.ShutDown.TabIndex = 1;this.ShutDown.Text = "关机";this.ShutDown.Click += new System.EventHandler(this.ShutDown_Click);//// Exit//this.Exit.Location = new System.Drawing.Point(216, 16); = "Exit";this.Exit.TabIndex = 2;this.Exit.Text = "退出";this.Exit.Click += new System.EventHandler(this.Exit_Click);//// Form1//this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(304, 53);this.Controls.Add(this.Exit);this.Controls.Add(this.ShutDown);this.Controls.Add(this.Reboot); = "Form1";this.Text = "重启、关机";this.ResumeLayout(false);//-----------------主函数,没有此函数可运行不了哦----------[STAThread]static void Main(){Application.Run(new Form1());}//--------------------重启部分程序private void Reboot_Click(object sender, System.EventArgs e){obj = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");foreach(ManagementObject oc in obj.Get()){DialogResult result = MessageBox.Show("Are you sure want to reboot?","Confirm",MessageBoxB uttons.OKCancel,rmation);//这里为确定用户是否点击“确定”按钮if(DialogResult.OK == result){oc.InvokeMethod("Reboot",null);//点击“确定”按钮后就调用此方法重启机器}}}//---------------关机部分程序------------------------private void ShutDown_Click(object sender, System.EventArgs e){if(DialogResult.OK == MessageBox.Show("Are you sure want to Shut Down?","Confirm",MessageB oxButtons.OKCancel,rmation)) //同上{//主要调用系统API来实现ExitWindowsEx(1,0);}}//这里的[DllImport("user32.dll")]是.net里属性的表达方法,DllImport为引用系统API所在的库" user32.dll"[DllImport("user32.dll")]//主要API是这个,注意:必须声明为static externprivate static extern int ExitWindowsEx(int x,int y);//这个是退出按钮的实现,即实现程序的退出private void Exit_Click(object sender, System.EventArgs e){Application.Exit();}}}c#实现关机,注销,重启源代码一. 设计关闭Windows窗体1. 界面的设计新建一个标准工程,向工程中增加一个Windows窗体并向窗体中添加如下控件,并分别设置其属性:控件名类别 Text 控件名类别TextCheckBox1 CheckBox 自动关机GroupBox1 GroupBox 当前系统时间CheckBox1 CheckBox 倒计时执行操作 GroupBox2 GroupBox 设定时间CheckBox1 CheckBox 定时报警 TxtTime TextBoxButCancle Button 取消 SetupTime DateTimePickerButReOpen Button 重新启动 SetupDate DateTimePickerButClose Button 关机Timer1 Timer 100ButSysInto Button 系统信息 ButReLogin Button 注消Windows窗体界面:将窗体属性中的caption设置为"关闭windows",名称设置为"frmmain"。
c语言自动关机程序代码

c语言自动关机程序代码
1.#include<stdlib.h>和#include<stdio.h>分别是C标准库文件,分别包含了系统函数和输入输出函数。
2. #include <windows.h> 是Windows API的头文件,包含了系统调用的函数和数据类型。
3. int main() 是程序的入口函数。
4. int delay; 定义了一个整型变量 delay。
5. printf('请输入关机延迟时间(单位:秒):'); 输出提示信息,让用户输入延迟时间。
6. scanf('%d', &delay); 从键盘读入一个整数,存放在变量delay 中。
7. printf('%d秒后将自动关机!', delay); 输出提示信息,告诉用户将在多少秒后关机。
8. Sleep(delay * 1000); 延迟指定的时间,此处单位为毫秒,需要将秒转换为毫秒。
9. system('shutdown -s -t 0'); 调用系统命令,执行关机操作。
其中,-s 表示关机,-t 0 表示立即关机。
10. return 0; 结束程序。
- 1 -。
c#实现强制关机、重启和注销

c#实现强制关机、重启、注销c#编程 2011-06-12 10:18:25 阅读0 评论0字号:大中小订阅//case "关闭计算机"://自动关闭主窗口,不需要密码,-f参数是强制执行Gloaljt.Isaq = false;System.Diagnostics.Process myProcess = newSystem.Diagnostics.Process();myProcess.StartInfo.FileName = "cmd.exe";eShellExecute = false;myProcess.StartInfo.RedirectStandardInput = true;myProcess.StartInfo.RedirectStandardOutput = true;myProcess.StartInfo.RedirectStandardError = true;myProcess.StartInfo.CreateNoWindow = true;myProcess.Start();myProcess.StandardInput.WriteLine("shutdown -s -f -t 0");// break;// case "重启计算机"://自动关闭主窗口,不需要密码Gloaljt.Isaq = false;System.Diagnostics.Process myProcesscq = new System.Diagnostics.Process();myProcesscq.StartInfo.FileName = "cmd.exe";eShellExecute = false;myProcesscq.StartInfo.RedirectStandardInput = true;myProcesscq.StartInfo.RedirectStandardOutput = true;myProcesscq.StartInfo.RedirectStandardError = true;myProcesscq.StartInfo.CreateNoWindow = true;myProcesscq.Start();myProcesscq.StandardInput.WriteLine("shutdown -r -f -t 0");break;// case "注销用户名"://注销用户,强制关闭程序Gloaljt.Isaq = false;System.Diagnostics.Process myProcesszx = new System.Diagnostics.Process();myProcesszx.StartInfo.FileName = "cmd.exe";eShellExecute = false;myProcesszx.StartInfo.RedirectStandardInput = true;myProcesszx.StartInfo.RedirectStandardOutput = true;myProcesszx.StartInfo.RedirectStandardError = true;myProcesszx.StartInfo.CreateNoWindow = true;myProcesszx.Start();myProcesszx.StandardInput.WriteLine("shutdown -l -f");// break;。
在C#中运用API函数编写多功能关机程序.doc

我用visual C#来编写一个多功能的关机程序。
该程序具有:定时关机、倒计时关机、关机 提醒、系统信息获取等四项功能,可设宦关机时间精确到秒。
并且让你很快掌握Visual C#H' 对API 的操作程序。
编写过程如下: 一、设计关闭Windows 窗体1 •界面的设计:新建一个标准工程,向工程中增加一个Windows 窗体并向窗体中添加如下控件,并分别 设置其属性:控件名类别 Text 控件名类别 TextCheckBox 0动关机 GixwpBox 1 GroupBox 当前系统时间 CheckBox 倒计时执行操作 GroupBox2 GroupBox 设定时间 CheckBox 定时报警 TxtTime TextBoxB utCancle B utton 取消 SetupTime DateTimePicker ButReOpen Button 重新启动 SetupDate DateTimePickerButClose Button 关机 Timer 1 Timer 100ButSyslnto Button 系统信息 ButReLogin BuMon 注消Windows 窗体界面:将窗体属性中的caption 设置为“关闭windows”,名称设置为'Trmmain^o2. 在窗体类屮引用APl|函数API 两数是构筑Windows 应用程序的基石,是Windows 编程的必备利器。
每一种 Windows 应用程序开发工具都提供了间接或直接调用了 Windows API 〔函数的方法,或者是 调用Windows API I®数的接口,也就是说具备调用动态连接库的能力。
VisualC#和其它开 发工具一样也能够调用动态链接库的API 函数。
在Visual C#中调用APl|的基本过程:首先,在调用APl|Z 前,你必须先导入System.Runtime.InteropServices 这个名称空间。
c语言中控制关机代码

C语言中命令如下:
重启
#include <stdlib.h>
main()
{
system("shutdown /r");
}
关机
#include <stdlib.h>
main()
{
system("shutdown /s");
}
XP系统下
else if (!ExitWindowsEx(EWX_POWEROFF, 0))
if(!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE, 0))
return FALSE;
return TRUE;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-l 注销(不能与选项 -m 一起使用)
-s 关闭此计算机
-r 关闭并重启动此计算机
-a 放弃系统关机
-m \\computername 远程计算机关机/重启动/放弃
关机代码(C语言)

关机代码(C语言)在计算机编程中,关机代码是一种能够让计算机自动关机的程序。
这种程序通常用于在特定条件下自动关闭计算机,比如在完成某项任务后自动关闭或者在系统出现故障时自动关闭。
在C语言中,编写关机代码并不复杂。
下面我们将介绍如何使用C语言编写一个简单的关机程序。
首先,我们需要包含Windows系统的头文件,因为关机功能是由操作系统提供的。
在C语言中,可以使用windows.h头文件来调用Windows系统的API函数。
接下来,我们需要定义一个主函数main(),在这个函数中我们将调用Windows系统的API函数来实现关机功能。
我们可以使用ExitWindowsEx函数来实现关机功能。
这个函数的原型如下:BOOL ExitWindowsEx(UINT uFlags, DWORD dwReason);其中,uFlags参数用于指定关机的方式,比如立即关机、重启、注销等。
dwReason参数用于指定关机的原因,比如系统故障、应用程序要求等。
下面是一个简单的关机程序的示例代码:```c。
#include <windows.h>。
int main() {。
// 关机。
ExitWindowsEx(EWX_SHUTDOWN, 0);return 0;}。
```。
在这个示例代码中,我们调用ExitWindowsEx函数并传入EWX_SHUTDOWN参数来实现关机功能。
当程序执行到这一行时,计算机将会自动关机。
除了简单的关机功能,我们还可以根据需要编写更复杂的关机程序。
比如可以添加判断条件,只有在特定的条件下才执行关机操作。
或者可以添加日志功能,记录每次关机的原因和时间。
总的来说,使用C语言编写关机代码并不复杂。
只需要了解Windows系统的API函数和C语言的基本语法,就可以轻松实现关机功能。
当然,在实际编写关机程序时,还需要考虑到系统稳定性和安全性等因素,确保关机操作不会对系统造成损害。
在实际应用中,关机代码可以用于自动化任务的执行。
C#实现windows系统重启、关机

C#实现windows系统重启、关机⼀、C#实现windows系统重启、关机实现原理,使⽤系统shutdown命令执⾏:强制关机:shutdown -s -f -t 0强制重启:shutdown -r -f -t 0C#实现代码封装:public class ShutdownHelper{///<summary>///到期时间///</summary>public DateTime End { get; set; }public ShutdownHelper(){DateTime now = DateTime.Now;DateTime end = now.Date.AddDays(1).AddSeconds(-1);this.End = end;}public ShutdownHelper(DateTime time){this.End = time;}//执⾏命令public void Exec(string str){try{using (Process process = new Process()){process.StartInfo.FileName = "cmd.exe";//调⽤cmd.exe程序eShellExecute = false;process.StartInfo.RedirectStandardInput = true;//重定向标准输⼊process.StartInfo.RedirectStandardOutput = true;//重定向标准输出process.StartInfo.RedirectStandardError = true;//重定向标准出错process.StartInfo.CreateNoWindow = true;//不显⽰⿊窗⼝process.Start();//开始调⽤执⾏process.StandardInput.WriteLine(str + "&exit");//标准输⼊str + "&exit",相等于在cmd⿊窗⼝输⼊str + "&exit"process.StandardInput.AutoFlush = true;//刷新缓冲流,执⾏缓冲区的命令,相当于输⼊命令之后回车执⾏process.WaitForExit();//等待退出process.Close();//关闭进程}}catch{}}//执⾏关机操作public void Shutdown(){this.Exec("shutdown -s -f -t 0");}//执⾏重启操作public void Restart(){this.Exec("shutdown -r -f -t 0");}//取消任务}⼆、关于shutdown命令详解:C#实现控制Windows系统关机、重启和注销的⽅法:shutdown命令的参数:shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前⽤户shutdown.exe -s -t 时间:设置关机倒计时shutdown.exe -h:休眠shutdown.exe -t 时间:设置关机倒计时。
c语言实现远程关机代码

case 5:system("shutdown -h -f");break;
case 6:
{
printf("输入远程计算机的IP或计算机名称:");
scanf("%s",mb);
system(strcat(p,mb));
break;
}
void main()
{
char cmd[20]="shutdown -s -t";
char t[5]="0",mb[16]="0";
char p[30]="shutdown -r -m ";
char q[40]="shutdown -s -m ";
int c;
system("title关机软件");//设置cmd窗口标题
}
case 7:
{
printf("输入要关闭的远程计算机名称或IP地址:");
scanf("%s",mb);
system(strcat(q,mb));
break;
}
case 8:break;
default:printf("Error!\n");
}
system("pause");
exit(0);
}
switch(c)
{
case 1:
{
printf("您想在多少秒后自动关闭计算机?(0~600)\n");
scanf("%s",t);
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
用c语言编写的关机程序本例定10分钟内关机。
可以自己设定关机时间。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
charcmd[20]="shutdown -s -t ";
char t[5]="0";
int c;
system("title C语言关机程序"); //设置cmd窗口标题
system("mode con cols=48 lines=25"); //窗口宽度高度
system("color f0"); //可以写成red 调出颜色组
system("date /T");
system("TIME /T");
printf("----------- C语言关机程序-----------\n");
printf("1.实现10分钟内的定时关闭计算机\n");
printf("2.立即关闭计算机\n");
printf("3.注销计算机\n");
printf("0.退出系统\n");
printf("-------------------------------------\n");
scanf("%d",&c);
switch(c) {
case 1:
printf("您想在多少秒后自动关闭计算机?(0~600)\n");
scanf("%s",t);
system(strcat(cmd,t));
break;
case 2:
system("shutdown -p");
break;
case 3:
system("shutdown -l");
break;
case 0:
break;
default:
printf("Error!\n"); }
system("pause"); return 0;
}。