您的位置:首页 > 移动开发 > 微信开发

自己写的关机、注销、重启小程序

2012-06-23 22:25 435 查看
无聊的时候自己写了个关于关机、注销、重启的程序..

主要的界面如下:



主要的原理是调用计算机里面的: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();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: