using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace HuaPuIEReStartService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
/// <summary>
/// 设定启动时间点
/// </summary>
private string starttime = "18:11";
Timer timer = new Timer();
protected override void OnStart(string[] args)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\huapuielog.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");
}
timer.Interval = 12000; //设置计时器事件间隔执行时间
timer.Elapsed += new ElapsedEventHandler(ResStartIE);
timer.Enabled = true;
}
/// <summary>
/// 服务停止
/// </summary>
protected override void OnStop()
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\huapuielog.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");
}
this.timer.Enabled = false;
}
/// <summary>
/// 调用Bat文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ResStartIE(object sender, System.Timers.ElapsedEventArgs e)
{
//当前时分秒
string nowtime = DateTime.Now.ToString("HH:mm");
// if (starttime == nowtime)
// {
//相对路径
string targetDir = string.Format(@"D:\Other\浏览器定时重启服务\定时执行Bat\");//this is where testChange.bat lies
Process proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = "重启浏览器.bat";
proc.StartInfo.UseShellExecute = true;
// proc.StartInfo.Arguments = string.Format("10");
proc.Start();
proc.WaitForExit();
// }
// MessageBox.Show("bat文件执行成功!");
}
}
}
资源地址:https://download.csdn.net/download/oDJMoney/86370572
还没有评论,来说两句吧...