在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 定时关机程序源码

C# 定时关机程序源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.07M
  • 下载次数:59
  • 浏览次数:389
  • 发布时间:2015-09-16
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C# 定时 定时关机

实例介绍

【实例简介】

【实例截图】

【核心代码】


namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        string cmdState="nothing";
        int goTime = 0;
        int countTime;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            notifyIcon1.Icon = this.Icon;
            textBox1.Text = DateTime.Now.ToString();
            numericUpDown1.Maximum = 23;
            numericUpDown2.Maximum = 59;
            numericUpDown3.Maximum = 59;
            numericUpDown4.Maximum = 23;
            numericUpDown5.Maximum = 59;
            numericUpDown6.Maximum = 59;
            comboBox1.Text = DateTime.Now.Month.ToString();
            comboBox2.Text = DateTime.Now.Day.ToString();
            numericUpDown1.Value = DateTime.Now.Hour;
            numericUpDown2.Value = DateTime.Now.Minute;
            numericUpDown3.Value = DateTime.Now.Second;
            numericUpDown4.Enabled = false;
            numericUpDown5.Enabled = false;
            numericUpDown6.Enabled = false;
            timer1.Interval = 1000;
            timer1.Enabled = true;
            //timer2.Interval = 500;
            //timer2.Enabled = true;
            textBox2.Enabled = false;
            for (int i = 1; i < 13; i  )
            {
                comboBox1.Items.Add(i);
            }
            for (int i = 1; i < 32; i  )
            {
                comboBox2.Items.Add(i);
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                numericUpDown4.Enabled = false;
                numericUpDown5.Enabled = false;
                numericUpDown6.Enabled = false;
                comboBox1.Enabled = true;
                comboBox2.Enabled = true;
                numericUpDown1.Enabled = true;
                numericUpDown2.Enabled = true;
                numericUpDown3.Enabled = true;
                numericUpDown1.Focus();
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked==true) 
            {
                numericUpDown4.Enabled = true;
                numericUpDown5.Enabled = true;
                numericUpDown6.Enabled = true;
                numericUpDown1.Enabled = true;
                comboBox1.Enabled = false;
                comboBox2.Enabled = false;
                numericUpDown1.Enabled = false;
                numericUpDown2.Enabled = false;
                numericUpDown3.Enabled = false;
                numericUpDown4.Focus();
            }
        }

        private void notifyShow() //显示托盘
        {
            this.Show();
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void notifyHide() //隐藏托盘
        {
            this.Hide();
            this.ShowInTaskbar = false;
            notifyIcon1.Visible = true;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                notifyHide();
        }

        public void cancelPlan()
        {
            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;
            cmdState = "nothing";
            goTime = 0;
            textBox3.Text = "0";
            MessageBox.Show("恭喜!任务已被取消!","取消任务",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
        }

        string f_getValue() //获取txt2的值,因为C#无法直接传递
        {
            return textBox2.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (button1.Enabled == false)
            {
                if (checkBox1.Enabled == false)
                {
                    Form2 f2 = new Form2();
                    f2.getvalue  = new Form2.getvalueHandler(f_getValue);
                    f2.ShowDialog();
                    System.Diagnostics.Debug.Print(f2.boolPwd.ToString());
                    if (f2.boolPwd.ToString() == "1")
                    {
                        textBox2.Enabled = true;
                        checkBox1.Enabled = true;
                        cancelPlan();
                    }
                    f2.Close();
                    f2.Dispose();
                }
                else
                {
                    cancelPlan();
                }
            }
            else
            {
                MessageBox.Show("注意!请新建任务后再执行取消操作!", "新建任务", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Text = DateTime.Now.ToString();
            if (cmdState != "nothing")
            {
                goTime  = 1;
                if (countTime - goTime   1 < 0)
                {
                    textBox3.Text = "0";
                    cmdState = "nothing";
                }
                else
                {
                    textBox3.Text = (countTime - goTime   1) / 86400   "天 "   (countTime - goTime   1) % 86400 / 3600   "时 "   (countTime - goTime   1)  % 3600 / 60   "分 "   (countTime - goTime   1) % 60   "秒";
                    if (countTime - goTime   1 == 0)
                    {
                        System.Diagnostics.ProcessStartInfo p1 = new System.Diagnostics.ProcessStartInfo("shutdown.exe", cmdState);
                        p1.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;   //隐藏窗口
                        System.Diagnostics.Process.Start(p1);
                        Application.ExitThread();
                        Application.Exit();
                    }
                }
            }
        }

        private void timeControl()
        {
            if (radioButton1.Checked == true)
            {
                DateTime startTime =Convert.ToDateTime (DateTime.Now.Year.ToString()   "/"   comboBox1.Text   "/"   comboBox2.Text   " "   numericUpDown1.Value   ":"   numericUpDown2.Value   ":"   numericUpDown3.Value);
                TimeSpan endTime = startTime-DateTime.Now; //旧的时间减去新的时间
                countTime =endTime.Days*86400  endTime.Hours * 3600   endTime.Minutes * 60   endTime.Seconds; //转为秒

            }
            else
            {
                countTime = decimal.ToInt32(numericUpDown4.Value) * 3600   decimal.ToInt32(numericUpDown5.Value) * 60   decimal.ToInt32(numericUpDown6.Value);
            }
            if(countTime<0)
            {
                MessageBox.Show("抱歉!您输入的时间已经过期,请重新输入!", "时间有误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if(checkBox1.Checked=true && textBox2.Text != "")
                {
                    textBox2.Enabled=false;
                    checkBox1.Enabled=false;
                }
                goTime=0;
                button1.Enabled=false;
                button2.Enabled=false;
                button3.Enabled=false;
            }
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int n = int.Parse(comboBox1.Text);
                if (n < 1 || n > 12)
                    comboBox1.Text = DateTime.Now.Month.ToString();
            }
            catch (Exception)
            {
                comboBox1.Text = DateTime.Now.Month.ToString();
            }
        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int n = int.Parse(comboBox2.Text);
                if (n < 1 || n > 31)
                {
                    comboBox2.Text = DateTime.Now.Day.ToString();
                }
                else
                {
                    if (comboBox1.Text == "2")
                    {
                        if (DateTime.IsLeapYear(DateTime.Now.Year) == true) //如果是闰年二月
                        {
                            if (Convert.ToInt32(comboBox2.Text) > 28)
                                comboBox2.Text = "28";
                        }
                        else
                        {
                            if (Convert.ToInt32(comboBox2.Text) > 29)
                                comboBox2.Text = "29";
                        }
                    }
                    else if (comboBox1.Text == "4" || comboBox1.Text == "6" || comboBox1.Text == "9" || comboBox1.Text == "11")
                    {
                        if (Convert.ToInt32(comboBox2.Text) > 30)
                            comboBox2.Text = "30";
                    }
                }
            }
            catch (Exception)
            {
                comboBox2.Text = DateTime.Now.Day.ToString();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.notifyIcon1.Icon = null;
            this.notifyIcon1.Visible = false;
            Application.Exit();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                textBox2.Enabled = true;
                textBox2.Focus();
            }
            else
            {
                textBox2.Enabled=false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timeControl();
            cmdState = "-s -f -t 0"; //关机
            label4.Text = "剩余关机时间";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timeControl();
            cmdState = "-l"; //注销
            label4.Text = "剩余注销时间";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timeControl();
            cmdState = "-r -f -t 0"; // 重启
            label4.Text = "剩余重启时间";
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            notifyShow();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            notifyShow();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            notifyHide();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://hi.baidu.com/flydoos/blog/item/a670c17ee0369b1b28388ab1.html");
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            notifyHide();
            Application.Exit();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = new System.IntPtr(5);
            //System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = new System.IntPtr(5);
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            MessageBox.Show("关机长老 2.0\n\n更新于2011-03-01", "关于");
        }

    }
}


标签: C# 定时 定时关机

实例下载地址

C# 定时关机程序源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警