首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

给自个儿写的定时关机程序

2012-12-20 
给自己写的定时关机程序效果图连接:http://http://bbs.csdn.net/topics/new?forum_idCSharp[最优解释]功

给自己写的定时关机程序
效果图

连接:http://http://bbs.csdn.net/topics/new?forum_id=CSharp
[最优解释]
功能很简单啊
代码写得怎么样呢?


[其他解释]
提几点意见:

1.  左图中时间设定处,时、分、秒之间缺少:分隔。

2.  个人认为,时、分、秒设定,用+-比用下拉框更人性化。

3.  这里的几个数字右对齐会美观一些
[其他解释]
还要输入精确到秒,我计算不够好怎么办
[其他解释]
几秒后关机很多余
[其他解释]
额,多了http
http://bbs.csdn.net/topics/new?forum_id=CSharp
[其他解释]
凌乱了
http://download.csdn.net/detail/wltica/4803757
[其他解释]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AotuShutdownPC
{
    public partial class Shutdown : Form
    {
        public Shutdown()
        {
            InitializeComponent();
        }
        int Delay;
        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.FileName = "cmd.exe";
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.RedirectStandardInput = true;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.StartInfo.RedirectStandardError = true;
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
            if (tabControl1.SelectedIndex == 0)
            {
                DateTime From = System.DateTime.Now.Date;
                DateTime To = dateTimePicker1.Value.Date;
                TimeSpan ts =To.Subtract(From);
                Delay = ts.Days * 24 * 3600-System.DateTime.Now.Hour * 3600 - System.DateTime.Now.Minute * 60 - System.DateTime.Now.Second


                        + Convert.ToInt32(comboBoxH.Text) * 3600 + Convert.ToInt32(comboBoxM.Text) * 60
                        + Convert.ToInt32(comboBoxS.Text);
                if (Delay >= 0)
                {
                    myProcess.StandardInput.WriteLine("shutdown -s -t " + Delay);
                }
                else
                {
                    MessageBox.Show("不能回到以前帮你关机,别傻了!");
                }
                
            }
            if(tabControl1.SelectedIndex==1)
            {
                Delay = Convert.ToInt32(textBox1.Text);
                myProcess.StandardInput.WriteLine("shutdown -s -t " + Delay);
                
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.FileName = "cmd.exe";
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.RedirectStandardInput = true;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.StartInfo.RedirectStandardError = true;
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();//启动进程
            
            myProcess.StandardInput.WriteLine("shutdown -a");//执行关机命令
        }
        
        private void Shutdown_Load(object sender, EventArgs e)


        {
            timer1.Interval = 1000;  
            timer1.Enabled = true;
            timer1.Start();
            label2.Text = "关机时间为: " + System.DateTime.Now.ToString(); 
            label1.Text = "当前时间: "+System.DateTime.Now.ToString();
            for (int h = 0; h < 25; h++)
            {
                comboBoxH.Items.Add(h.ToString());
            }
            comboBoxH.Text = comboBoxH.Items[0].ToString(); 
            for (int m = 0; m < 61; m++)
            {
                comboBoxM.Items.Add(m.ToString());
            }
            comboBoxM.Text = comboBoxM.Items[0].ToString();
            for(int s=0;s<61;s++)
            {
                comboBoxS.Items.Add(s.ToString());
            }
            comboBoxS.Text = comboBoxS.Items[0].ToString();
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8)
            {
                e.Handled = true;
            } 
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.ToString() != "" && Convert.ToInt32(textBox1.Text.Trim()) > 30758400)
            {
                MessageBox.Show("你确定要在一年以后关机???");
                textBox1.Text = "";
            }
            int LaterH=0;


            string ymd = System.DateTime.Today.ToString("yyyy/MM/dd"); ;
            int s=0;
            try
            {
                
                s = Convert.ToInt32(textBox1.Text.Trim());
                int NowS, NowM, NowH, NowD;
                int LaterS = 0;
                int LaterM = 0;
                NowD = System.DateTime.Now.Second;
                NowH = System.DateTime.Now.Hour;
                NowM = System.DateTime.Now.Minute;
                NowS = System.DateTime.Now.Second;
                LaterS = NowS + s % 60;
                LaterM = NowM + (s / 60)%60;
                LaterH = NowH +(s/60)/60;
                if (LaterS > 60)
                {
                    LaterS = LaterS - 60;
                    LaterM++;
                }
                if (LaterM > 60)
                {
                    LaterM = LaterM - 60;
                    LaterH++;
                }
                if (LaterH > 23)
                {
                    int  i = LaterH / 24;
                    ymd = System.DateTime.Now.AddDays(i).ToShortDateString();  
                   


                    LaterH = LaterH % 24;  
                }
                label2.Text = "关机时间为:"+ymd+" "+ LaterH + ":" + LaterM + ":" + LaterS;
            }
            catch 
            {
                label2.Text = "关机时间为:" + System.DateTime.Now.ToString(); 
            }
            
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            showLocalTime();
        }
        private void showLocalTime()
        {
            this.label1.Text = "当前时间: " + System.DateTime.Now.ToString();
        }
        private void comboBoxH_Leave(object sender, EventArgs e)
        {
            if (Convert.ToInt32(comboBoxH.Text) > 23 && comboBoxH.Text != "")
            {
                MessageBox.Show("火星来的吧!地球一天只有24小时");
                comboBoxH.Text = "0";
                comboBoxH.Focus();
            }
        }

        private void comboBoxM_Leave(object sender, EventArgs e)
        {
            if (Convert.ToInt32(comboBoxM.Text) > 59 && comboBoxM.Text != "")
            {
                MessageBox.Show("火星来的吧!地球一小时只有60分");
                comboBoxM.Text = "0";
                comboBoxM.Focus();
            }
        }

        private void comboBoxS_Leave(object sender, EventArgs e)


        {
            if (Convert.ToInt32(comboBoxS.Text) > 59 && comboBoxS.Text != "")
            {
                MessageBox.Show("火星来的吧!地球一分钟只有60秒");
                comboBoxS.Text = "0";
                comboBoxS.Focus();
            }
        }
    }
}


[其他解释]
两分太贵了,伤不起

热点排行