关机

using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
 
public class shutdown : MonoBehaviour {
 
    public string ShutDownDate = "19:00";
 
    bool Switch;
 
    void Awake()
    {
        OpenData();
    }
 
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            WindowsShutDown();
        }
 
        if (DateTime.Now.ToString("HH/mm").Contains(ShutDownDate))
        {
            if (!Switch)
            {
                Switch = true;
                WindowsShutDown();
            }
        }
 
    }
 
    void OpenData()
    {
        string path = Application.streamingAssetsPath + "/data.txt";
        if (File.Exists(path))
        {
            try
            {
                StreamReader streamReader = File.OpenText(path);
                ShutDownDate = streamReader.ReadToEnd();
                streamReader.Dispose();
            }
            catch (Exception e)
            {
                Debug.LogWarning(e.Message);
            }
        }
    }
 
    void WindowsShutDown()
    {
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "/c " + "shutdown -s -t 10";
        p.Start();
        WindowsMessage.AutoShutDown();
        Application.Quit();
    }
}
 
 
 
/// <summary>
/// 调用外部库
/// </summary>
public class WindowsMessage
{
    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern int MessageBox(IntPtr handle, String message, String title, int type);//具体方法
    public static void AutoShutDown()
    {
        WindowsMessage.MessageBox(IntPtr.Zero, "倒计时10秒之后关机", "关机,下班!", 0);
    }
}

 

posted @ 2021-03-02 14:35  All_Nighter  阅读(105)  评论(0编辑  收藏  举报