自己写的关机、注销、重启小程序
2012-06-23 22:25
435 查看
无聊的时候自己写了个关于关机、注销、重启的程序..
主要的界面如下:
主要的原理是调用计算机里面的:cmd.exe 这个
用法如下:
下面是代码部分:
主要的界面如下:
主要的原理是调用计算机里面的:cmd.exe 这个
用法如下:
下面是代码部分:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Text.RegularExpressions; namespace ShutdownWindows { public partial class DoWork : Form { private string regStr="^[0-9]*$"; private int second = 0; public DoWork() { InitializeComponent(); this.timer1.Stop(); } /// <summary> /// 执行操作 /// </summary> private void btnConfirm_Click(object sender, EventArgs e) { if (this.btnConfirm.Text == "执 行") { if (this.txtTime.Text.Trim() == "") { MessageBox.Show("请输入时间", "ERROR", MessageBoxButtons.OK); return; } rbtnLogoff.Enabled = rbtnReStart.Enabled = rbtnShutdown.Enabled = txtTime.Enabled = false; this.btnConfirm.Text = "中 止"; second = Convert.ToInt32(this.txtTime.Text.Trim()) * 60; this.timer1.Start(); } else { this.timer1.Stop(); this.txtTime.Text = ""; this.txtTime.Focus(); rbtnLogoff.Enabled = rbtnReStart.Enabled = rbtnShutdown.Enabled = txtTime.Enabled = true; this.btnConfirm.Text = "执 行"; this.lblInfo_4.Text = "输入时间进行操作!"; Work("4"); } } /// <summary> /// 操作方法 /// </summary> /// <param name="type">操作类型 1:关机 2:重启 3:注销 4:中止计算机执行的操作</param> /// <param name="time">执行时间(秒)</param> private void Work(string type) { Process pro = new Process();//实例化一个独立的进程 pro.StartInfo.FileName = "cmd.exe";//进程的操作对象名称 pro.StartInfo.UseShellExecute = false;//是否启动系统外壳 pro.StartInfo.RedirectStandardInput = true;//执行的命令是否从StandardInput流中输入 pro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.CreateNoWindow = true;//启动时是否显示窗体 pro.Start();//启动 switch (type) { case "1": pro.StandardInput.WriteLine("shutdown -p"); break; case "2": pro.StandardInput.WriteLine("shutdown -r"); break; case "3": pro.StandardInput.WriteLine("shutdown -l"); break; case "4": pro.StandardInput.WriteLine("shutdown -a"); break; } pro.StandardInput.WriteLine("exit"); pro.StandardOutput.ReadToEnd(); pro.Close(); } /// <summary> /// Timer 事件 /// </summary> private void timer1_Tick(object sender, EventArgs e) { string type = ""; string info = "系统将在:" + second.ToString() + "秒后执行:"; if (rbtnLogoff.Checked) { type = "3"; info += "注销"; } if (rbtnReStart.Checked) { type = "2"; info += "重启"; } if (rbtnShutdown.Checked) { type = "1"; info += "关机"; } this.lblInfo_4.Text = info; if (second == 0) { Work(type); } info += "操作"; second = second - 1; } /// <summary> /// 文本事件 /// </summary> private void textBox1_TextChanged(object sender, EventArgs e) { Regex reg = new Regex(regStr); if (!reg.IsMatch(this.txtTime.Text.Trim())) { MessageBox.Show("请输入数字", "ERROR", MessageBoxButtons.OK); this.txtTime.Text = ""; } } /// <summary> /// 退出按钮事件 /// </summary> private void btnClose_Click(object sender, EventArgs e) { DialogResult dr = MessageBox.Show("确认退出吗?", "确认信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); if (dr == DialogResult.Yes) { Work("4"); this.Close(); } } } }
相关文章推荐
- C#程序运行时,WINDOWS不能注销、重启或关机
- 定时注销、关机、重启的小程序
- C#程序注销、重启、关机和锁定电脑
- 如何让自己的应用程序,控制台程序,windows 服务收到操作系统注销或者重启的消息
- java开发_windows下面的关机,重启,注销小程序_源码下载
- 我写了3个快速关机、注销、重启的程序
- VB关机程序--包括注销、重启、关机
- Tips: 如何让自己的应用程序,控制台程序,windows 服务收到操作系统注销或者重启的消息?
- C#电脑重启,关机,注销
- C++中在程序中重启自己的一种方法
- C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠
- linux 系统关机 重启与注销命令
- Android 自己的程序中添加系统重启的提示框
- CentOS6.6的重启、注销、关机命令
- c# 中 控制电脑的 待机 关机 注销 重启 等操作的实现
- C++写出关机、重启、注销、休眠等操作:
- 让电脑重启,关机和注销的c#源程序
- C# 调用系统关机、重启、注销的类
- ubuntu 14.04 anm64修复 不能关机、重启、注销的问题
- C# 控制本地计算机注销,重启,关机命令