using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
int i = 0;
private int ti;
public Form1()
{
InitializeComponent();
timer1.Enabled = false;
}
public string CmdPc(string cmdinput)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine(cmdinput);
p.StandardInput.WriteLine("exit");
string ss=p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return ss;
}
catch
{
string ss = "命令执行失败";
return ss;
}
}
private void button1_Click(object sender, EventArgs e)
{
//label1.Text = ClosePc();
if (textBox2.Text != "")
{
ti = Convert.ToInt32(textBox2.Text);
timer1.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button1.Enabled = false;
}
else { label4.Text = "请输入时间,单位/分钟"; }
}
private void timer1_Tick(object sender, EventArgs e)
{
if (ti - i == 1)//从5开始到计时
{
timer1.Enabled = false;
label4.Text = "时间到,关机!!";
label1.Text = CmdPc("Shutdown.exe -s -f");
button4.Visible = true;
}
else
{
i++;
string j = (ti - i).ToString();
label4.Text = j;
}
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
button4.Enabled = true;
button1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text=CmdPc(textBox1.Text);
}
private void button4_Click_1(object sender, EventArgs e)
{
timer1.Enabled = false;
button4.Enabled = false;
button1.Enabled = true;
label4.Text = "停止";
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
ti = Convert.ToInt32(textBox2.Text);
timer1.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button1.Enabled = false;
}
}
}
}
01
2012
04
C#写的自动关机及执行CMD命令
发布:郑德才博客 | 分类:学习之路 | 评论:0 | 浏览:
相关文章:
C#、Asp.Net 对比两个实体信息前后是否有所改变(结构相同和不相同) (2016-9-2 20:24:29)
C#、Asp.Net 将一个实体对象转换为另一个实体对象(结构可以不一样) (2016-9-2 19:14:5)
【C#、Asp.Net 工具类大全】图片通用操作类 (2015-12-23 13:27:30)
【C#、Asp.Net 工具类大全】Request请求工具类 (2015-12-23 13:15:56)
【C#、Asp.Net 工具类大全】弹出提示操作类 (2015-12-23 13:6:1)
【C#、Asp.Net 工具类大全】正则匹配工具类 (2015-7-26 9:36:56)
【C#、Asp.Net 工具类大全】Js常用操作类 (2015-7-15 14:23:33)
【C#、Asp.Net 工具类大全】Html常用帮助类 (2015-7-11 23:42:53)
C#字符串编码帮助类 (2015-7-11 23:39:38)
【C#、Asp.Net 工具类大全】压缩文本、字节或者文件的压缩辅助类 (2015-7-11 23:23:49)
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。