無聊的時候自己寫了個關於關機、註銷、重啓的程序..
主要的界面如下:
主要的原理是調用計算機裏面的: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();
}
}
}
}