C语言写定时关机重启小程序
- 格式:doc
- 大小:97.50 KB
- 文档页数:4
C语⾔(关机⼩程序)关机⼩程序利⽤system写出且复习借鉴于⽼九学堂C语⾔笔记:1 #include<stdio.h>2 #include<stdlib.h>3 #include<string.h>4int print()5 {6 printf("++++++关机⼩程序+++++\n");7 printf("1、定时关机(10分钟以内),请注意需要保存的⽂件\n");8 printf("2、⽴即关闭计算机系统\n");9 printf("3、注销系统\n");10 printf("0、推出程序\n");11return0;12 }13void main()14 {15 system("title C语⾔关机程序");16 system("mode con cols=48 lines=25");17 system("color 72");18 system("date /T");19 system("TIME /T");20char cmd[20]="shutdown -s -t";21char t[5]="0";22 print();23int choose;24 printf("请选择关机的选项:\n");25 scanf("%d",&choose);26 getchar();27switch(choose)28 {29case1:30 printf("输⼊⼏秒后关机,进⾏⾃动关机?(0~600秒)\n");31 scanf("%d",&t);32 system(strcat(cmd,t));33break;34case2:35 system("shutdown -p");36break;37case3:38 system("shutdown -1");39break;40case0:41break;42default:43 printf("选择错误\n");44 }45 system("pause");46 exit(0);47 }48 ```49希望⼤家能喜欢⼀起来进步学习。
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语言自动关机程序代码
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#编程 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;。
我用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 这个名称空间。