C# Set automatic shutdown

Function: Automatically set the computer shutdown time, which can be canceled

Create a shutdown function, bool isCancle, if the input value is true, the shutdown will be canceled, interval interval time, unit unsigned integer type

  private static void ShutdownPC(bool isCancel, uint interval)
         {
            Process proc = new Process();
            proc.StartInfo.FileName = "cmd.exe"; // 启动命令行程序
            proc.StartInfo.UseShellExecute = false; // 不使用Shell来执行,用程序来执行
            proc.StartInfo.RedirectStandardError = true; // 重定向标准输入输出
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.CreateNoWindow = true; // 执行时不创建新窗口
            proc.Start();
            string commandLine;
            if (isCancel)
                commandLine = @"shutdown /a";//取消自动关机命令
            else
                commandLine = @"shutdown /s /t " + interval.ToString();
            proc.StandardInput.WriteLine(commandLine);
         }

1 time selector, 2 buttons, an OK button and a Cancel button

When the default time is less than the set time, an error will be prompted.

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_sure_Click(object sender, EventArgs e)
        {
           //延迟关机,等待的时间
           int waitSecond = 0;
           DateTime currentTime = DateTime.Now.ToLocalTime();//获取当前时间
           DateTime aimTime = dateTimePicker_aim.Value;//获取目标时间
           //比较两个时间。关机时间也在当前时间之后
           if (DateTime.Compare(currentTime, aimTime) >= 0)
              {
                 MessageBox.Show("目标时间小于当前时间");
                 return;
               }
           else{
                //将两个时间间隔,换算成秒,取整输出
                int AimCompareCurrentToSecond = ((int)((aimTime - currentTime).TotalSeconds));
                waitSecond = AimCompareCurrentToSecond + 1;
                //MessageBox.Show(AimCompareCurrentToSecond.ToString());
                }          
            //设置自动关机
            ShutdownPC(false, (uint)waitSecond);
            label_current.Text = "已设置自动关机";
            button_cancel.Enabled = true;
            button_sure.Enabled = false;
        }
        /// <summary>
        /// 取消关机按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            ShutdownPC(true, 0);//true取消自动关机,0关机延迟时间
            label_current.Text = "无自动关机";
            button_cancel.Enabled = false;
            button_sure.Enabled = true;

        }

Interface effect:

Complete source code:

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

namespace Auto_shut
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         private static void ShutdownPC(bool isCancel, uint interval)
         {
            Process proc = new Process();
            proc.StartInfo.FileName = "cmd.exe"; // 启动命令行程序
            proc.StartInfo.UseShellExecute = false; // 不使用Shell来执行,用程序来执行
            proc.StartInfo.RedirectStandardError = true; // 重定向标准输入输出
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.CreateNoWindow = true; // 执行时不创建新窗口
            proc.Start();
            string commandLine;
            if (isCancel)
                commandLine = @"shutdown /a";//取消自动关机命令
            else
                commandLine = @"shutdown /s /t " + interval.ToString();
            proc.StandardInput.WriteLine(commandLine);
         }
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_sure_Click(object sender, EventArgs e)
        {
           //延迟关机,等待的时间
           int waitSecond = 0;
           DateTime currentTime = DateTime.Now.ToLocalTime();//获取当前时间
           DateTime aimTime = dateTimePicker_aim.Value;//获取目标时间
           //比较两个时间。关机时间也在当前时间之后
           if (DateTime.Compare(currentTime, aimTime) >= 0)
              {
                 MessageBox.Show("目标时间小于当前时间");
                 return;
               }
           else{
                //将两个时间间隔,换算成秒,取整输出
                int AimCompareCurrentToSecond = ((int)((aimTime - currentTime).TotalSeconds));
                waitSecond = AimCompareCurrentToSecond + 1;
                //MessageBox.Show(AimCompareCurrentToSecond.ToString());
                }          
            //设置自动关机
            ShutdownPC(false, (uint)waitSecond);
            label_current.Text = "已设置自动关机";
            button_cancel.Enabled = true;
            button_sure.Enabled = false;
        }
        /// <summary>
        /// 取消关机按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            ShutdownPC(true, 0);//true取消自动关机,0关机延迟时间
            label_current.Text = "无自动关机";
            button_cancel.Enabled = false;
            button_sure.Enabled = true;

        }

    }
}

Guess you like

Origin blog.csdn.net/daisy_juhua/article/details/133070946