C#写的自动关机及执行CMD命令

 

下载地址:http://download.csdn.net/source/451364

晚上的时候太困,想躺床上看电影,怕睡着,

就在网上下载了个自动关机的软件,测试了一下,结果不好用

于是乎我的精神头来了,写了个自动关机的,在网上查了下执行CMD部分的内容后就写了个,自给自足,呵

我是新手,写出来后还真蛮有成就感的,哈。提供给大家。

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;
            }

        }




    }

}

 

下载地址:http://download.csdn.net/source/451364

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值