优雅的帮手小可爱关机-基于c#的关机小助手

277 阅读3分钟

我正在参加「创意开发 投稿大赛」详情请看:掘金创意开发大赛来了!

  • 关机谁不会啊,点点鼠标就好了。定时关机呢? cmd+ shutdown -s -t 60 ? 你确定你的小可爱会这些吗?就算会,每次还要算一下时间,转换单位,如果设置成功了,又突然想取消咋办?女朋友就算小可爱,就是宝宝。你让你的宝宝考虑这么多的问,你不心疼吗?
  • 所以,一个有爱的自动关机小助手上线了!
  • 先看界面,虽然简陋,甚至带有土气。但是这正好反应了直男程序员的浪漫啊!

image.png

  • 系统环境

  • 基于Windows Form的c#开发

  • 先说功能 功能主要分为指定时间关机 和倒计时关机,指定时间关机就是在一个特定的时间关机,比如下班5.30, 倒计时关机就是指定时间后关机。所有的关机都持取消。

  • 后端代码 主要包括初始化界面配置,具体的关机逻辑通过子进行调用cmd程序,然后执行对应的命令,这样的好处是方便我们修改配置,和利用成熟的软件。 在指定时间关机时关机时间不能小于当前时间。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dateTimePicker1.Enabled = false;
            comboBox1.Enabled = false;

            //先构造一个dataTable,或者从数据库读取到一个,这里自己构造一个
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Number", typeof(double));
            dataTable.Columns.Add("ShowText", typeof(String));
            dataTable.Rows.Add(new String[] { "10", "10分钟" });
            dataTable.Rows.Add(new String[] { "30", "30分钟" });
            dataTable.Rows.Add(new String[] { "60", "60分钟" });
            dataTable.Rows.Add(new String[] { "120", "2小时" });
            dataTable.Rows.Add(new String[] { "240", "4小时" });
            dataTable.Rows.Add(new String[] { "360", "6小时" });

            comboBox1.DataSource = dataTable;//绑定
            comboBox1.DisplayMember = dataTable.Columns[1].ColumnName;//显示的文本
            comboBox1.ValueMember = dataTable.Columns[0].ColumnName;//对应的值
            button1.Enabled = false;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Enabled = true;
            comboBox1.Enabled = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            comboBox1.Enabled = true;
            dateTimePicker1.Enabled = false;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            TimeSpan ts = dateTimePicker1.Value.Subtract(DateTime.Now);
            if (ts.TotalSeconds < 0)
            {
                MessageBox.Show("关机时间不能小于当前时间");
                button1.Enabled = false;
            }
            else
            {
                button1.Enabled = true;
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            double myTime=0;
            if (radioButton1.Checked == true)
            {
                TimeSpan ts = dateTimePicker1.Value.Subtract(DateTime.Now);
                myTime = ts.TotalSeconds;
            }
            else if (radioButton2.Checked == true)
            {
                if (!double.TryParse(this.comboBox1.SelectedValue.ToString(), out myTime))
                {
                    MessageBox.Show("定时关机设置失败");
                    return;
                }
                else
                {
                    myTime = myTime * 60;
                }
            }
            else
            {
                MessageBox.Show("定时关机设置失败");
                return;
            }
            var startInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardInput = true ;
            startInfo.RedirectStandardOutput = true ;
            startInfo.RedirectStandardError = true  ;
            startInfo.CreateNoWindow = true;
            var myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo = startInfo;
            myProcess.Start();
            //myProcess.StandardInput.WriteLine("calc");
            DialogResult dr = MessageBox.Show("确认关机吗?", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                //用户选择确认的操作
                //MessageBox.Show("您选择的是【确认】");
                myProcess.StandardInput.WriteLine("shutdown -a ");
                myProcess.StandardInput.WriteLine("shutdown -s -t " + (int)myTime);
                int myTime2 = (int)myTime / 60;
                MessageBox.Show("定时关机设置成功," + myTime2.ToString() + "分钟后将自动关机");
            }
            else if (dr == DialogResult.Cancel)
            {
                //用户选择取消的操作
                //MessageBox.Show("您选择的是【取消】");
            }
           
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var startInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            startInfo.CreateNoWindow = true;
            var myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo = startInfo;
            myProcess.Start();
            //myProcess.StandardInput.WriteLine("calc");
            DialogResult dr = MessageBox.Show("确认取消关机吗?", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                //用户选择确认的操作
                //MessageBox.Show("您选择的是【确认】");
                myProcess.StandardInput.WriteLine("shutdown -a ");
                MessageBox.Show("取消关机成功");
            }
            else if (dr == DialogResult.Cancel)
            {
                //用户选择取消的操作
                //MessageBox.Show("您选择的是【取消】");
            }
        }
    }
  • 界面主要配置 界面主要winfrom原生组件,尽力减少依赖,通过checkbox选择关机类型,时间关机初始默认为当前时间。定时关机依据预先配置节点进行选择。
#region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.button1 = new System.Windows.Forms.Button();
            this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.button2 = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(25, 205);
            this.button1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(69, 26);
            this.button1.TabIndex = 0;
            this.button1.Text = "确定关机";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(8, 69);
            this.radioButton2.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(83, 16);
            this.radioButton2.TabIndex = 1;
            this.radioButton2.TabStop = true;
            this.radioButton2.Text = "倒计时关机";
            this.radioButton2.UseVisualStyleBackColor = true;
            this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Location = new System.Drawing.Point(8, 30);
            this.radioButton1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(95, 16);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "指定时间关机";
            this.radioButton1.UseVisualStyleBackColor = true;
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // comboBox1
            // 
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] {
            "10分钟 ",
            "30分钟",
            "60分钟",
            "2小时",
            "4小时",
            "6小时"});
            this.comboBox1.Location = new System.Drawing.Point(153, 152);
            this.comboBox1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(92, 20);
            this.comboBox1.TabIndex = 3;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged)
        }

        #endregion
        
        
  • 后记,别说还真的好用,我自己也经常用。女朋友不用吗?女朋友的梦想是不上班,肯定不会用啦!贴一个马上关机的效果,今天的任务就完成了。
  • 拓展,虽然这个程序很小,很简单,但是这个地方有用到子进程喔,面试经常会问的线程和进程有什么不同,它们直接如何通信,都可以从这个地方发散出去。

image.png