- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PowerOffOnTime
- {
- class Program
- {
- //如果在Properties里面把output type 改为Windows Application,将没有任何可以改变的机会了,彻底崩溃。
- static void Main(string[] args)
- {
- StartWithWindows();
- ShutDown();
- }
- private static void StartWithWindows()
- {
- try
- {
- string appName = "PowerOffOnTime";
- string thisExecutablePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
- Microsoft.Win32.RegistryKey Rkey =
- Microsoft.Win32.Registry.LocalMachine.CreateSubKey
- ("SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run");
- Rkey.SetValue(appName, thisExecutablePath);
- Rkey.Close();
- }
- catch (Exception)
- {
- }
- }
- private static void ShutDown()
- {
- System.Diagnostics.Process.Start("shutdown.exe", "-a");
- System.Threading.Thread.Sleep(2000);
- Int64 timeSecond = getTotalSecondsAfterNow(21, 30, 0);// I want to power off in 21:30:00 everyday.
- if (timeSecond <= 0)
- {
- System.Diagnostics.Process.Start("shutdown.exe", "-f -s -t 0");
- return;
- }
- //System.Diagnostics.Process.Start("shutdown.exe", "-s -t " + timeSecond.ToString());
- //if we are workint very important,so we can not use -f in the next line.
- System.Diagnostics.Process.Start("shutdown.exe", "-f -s -t " + timeSecond.ToString());
- Environment.Exit(0);
- }
- private static Int64 getTotalSecondsAfterNow(int hour, int minute, int second)
- {
- return (Int64)(new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day,
- hour, minute, second, DateTimeKind.Local) - DateTime.Now)
- .TotalSeconds;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0709201513631.html
来源: http://www.codesnippet.cn/detail/0709201513631.html