自己玩的定时关机小软件

这些天晚上总是听着音乐睡觉,可是电脑不能开一晚上啊 ,就想下载个定时关机的

可没找到自己喜欢的,就自己写了一个用来玩的定时关机的

功能比较单一,自己用不错。呵呵

主要代码如下:

// 关机程序调用 cmd.exe 加传参
Process.Start( " cmd.exe " " /c shutdown /s /t 10 " );

 

// 计算时间间隔 TimeSpan
TimeSpan ts  =   new  TimeSpan(dtpSetTime.Value.Ticks  -  DateTime.Now.Ticks);

 

 完整代码如下:

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.Runtime.InteropServices;
using  System.Diagnostics;

namespace  定时关机
{
    
public   partial   class  frmMain : Form
    {
        
public  frmMain()
        {
            InitializeComponent();
        }

        
private   void  timer1_Tick( object  sender, EventArgs e)
        {
            
// 时间到了
             if  (dtpSetTime.Value.ToString()  ==  DateTime.Now.ToString())
            {
                
// 关机
                Process.Start( " cmd.exe " " /c shutdown /s /t 10 " );
                timer1.Enabled 
=   false ;

                
//
                dtpSetTime.Enabled  =   true ;
                btnDo.Text 
=   " 倒计时开始 " ;
                btnExitClose.Enabled 
=   true ;
                
return ;
            }

            TimeSpan ts 
=   new  TimeSpan(dtpSetTime.Value.Ticks  -  DateTime.Now.Ticks);

            lblInfo.Text 
=   " 距离关机还剩  "   +  ( int )ts.TotalHours  +   "  小时  "   +  ts.Minutes  +   "  分  "   +  ts.Seconds  +   "  秒  " ;
        }

        
private   void  Form1_Load( object  sender, EventArgs e)
        {
            dtpSetTime.Value 
=  DateTime.Now;
        }

        
private   void  btnStart_Click( object  sender, EventArgs e)
        {
            
// 时间设置是否许可
             if  (dtpSetTime.Value.Ticks  <=  DateTime.Now.Ticks)
            {
                timer1.Enabled 
=   false ;
                MessageBox.Show(
" 不能设置比现在还早的时间!时间不能倒流哦! " );
                
return ;
            }

            
if  ( ! timer1.Enabled)
            {
                timer1.Enabled 
=   true ;
                dtpSetTime.Enabled 
=   false ;
                btnDo.Text 
=   " 点击停止倒计时 " ;
            }
            
else
            {
                timer1.Enabled 
=   false ;
                dtpSetTime.Enabled 
=   true ;
                btnDo.Text 
=   " 倒计时开始 " ;
            }
        }

        
private   void  btnExitClose_Click( object  sender, EventArgs e)
        {
            Process.Start(
" cmd.exe " " /c shutdown /a " );
        }
    }
}

 

 

你可能感兴趣的:(软件)