最近一段时间笔记本电脑老是自动开机,拿去修也没修好,果断写个程序让电脑自动休眠
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public static string FILENAME =
"C://Myservice.log";
public static DateTime lastTimePoint;
public static string Execute(
string dosCommand)
{
return Execute(dosCommand,
10);
}
public static string Execute(
string command,
int seconds)
{
string output =
"";
if (command !=
null && !command.Equals(
""))
{
Process process =
new Process();
ProcessStartInfo startInfo =
new ProcessStartInfo();
startInfo.FileName =
"cmd.exe";
startInfo.Arguments =
"/C " + command;
startInfo.UseShellExecute =
false;
startInfo.RedirectStandardInput =
false;
startInfo.RedirectStandardOutput =
true;
startInfo.CreateNoWindow =
true;
process.StartInfo = startInfo;
try
{
if (process.Start())
{
if (seconds ==
0)
process.WaitForExit();
else
process.WaitForExit(seconds);
output = process.StandardOutput.ReadToEnd();
}
}
catch { }
finally
{
if (process !=
null)
process.Close();
}
}
return output;
}
public Service1()
{
InitializeComponent();
}
public static void LogFile(
string filename,
string buf)
{
try {
FileStream fs =
new FileStream(filename, FileMode.Append);
byte[] data = System.Text.Encoding.Default.GetBytes(buf +
"\r\n");
fs.Write(data,
0, data.Length);
fs.Flush();
fs.Close();
}
catch (Exception e)
{
Console.WriteLine(
"cannot open file");
}
}
private static void onTimeEvent(
object source, ElapsedEventArgs e)
{
DateTime now = DateTime.Now;
if (now > lastTimePoint.AddMinutes(
5))
{
LogFile(FILENAME,
"[!] 检测到电脑启动: " + now );
lastTimePoint = now;
setAutoCheck();
return;
}
lastTimePoint = now;
}
public static void setAutoCheck()
{
System.Timers.Timer t =
new System.Timers.Timer(
5 *
60 *
1000);
t.Elapsed +=
new System.Timers.ElapsedEventHandler(loginCheck);
t.AutoReset =
false;
t.Enabled =
true;
t.Start();
}
private static void loginCheck(
object source, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now > lastTimePoint.AddMinutes(
5))
{
String time = DateTime.Now.ToString();
LogFile(FILENAME,
"[!] 当前时间: " + time );
LogFile(FILENAME,
"[!] 计时器超时,自动失效");
return;
}
if(! isLogin())
{
String time = DateTime.Now.ToString();
LogFile(FILENAME,
"[!] 当前时间: "+time);
LogFile(FILENAME,
"[!] 非正常启动,将立即尝试自动休眠电脑");
LogFile(FILENAME, Execute(
"shutdown /h"));
}
}
public static bool isLogin()
{
string logType1 =
"Security";
EventLog e =
new EventLog(logType1);
int length = e.Entries.Count;
DateTime now = DateTime.Now;
for (
int i = length -
1; ; i--)
{
EventLogEntry l = e.Entries[i];
if (l.TimeGenerated > now.AddMinutes(-
10))
{
if (l.InstanceId ==
4624 && l.Message.Contains(
"bluekezhou@qq.com"))
{
LogFile(FILENAME,
"[*] 检测到登录事件:" +
l.InstanceId.ToString() +
" " +
l.TimeGenerated );
return true;
}
}
else
break;
}
LogFile(FILENAME,
"[*] 没有检测用户登录");
return false;
}
protected override void OnStart(
string[] args)
{
String time = DateTime.Now.ToString();
LogFile(FILENAME,
"\r\n" + time);
LogFile(FILENAME,
"[*] 服务启动");
lastTimePoint = DateTime.Now;
setAutoCheck();
System.Timers.Timer aTimer =
new System.Timers.Timer();
aTimer.Elapsed +=
new System.Timers.ElapsedEventHandler(onTimeEvent);
aTimer.AutoReset =
true;
aTimer.Interval =
60000;
aTimer.Enabled =
true;
aTimer.Start();
LogFile(FILENAME,
"[*] 监听器设置完毕");
}
protected override void OnStop()
{
String time = DateTime.Now.ToString();
LogFile(FILENAME,
"\r\n" + time);
LogFile(FILENAME,
"[*] 服务停止");
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-1309230.html