[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
/// <summary>
/// 开启服务时要执行的方法
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
}
/// <summary>
/// 停止服务时要执行的方法
/// </summary>
protected override void OnStop()
{
}
}
}
[C#] 纯文本查看 复制代码
//定期类
private System.Timers.Timer aTimer;
/// <summary>
/// 开启服务时要执行的方法
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
aTimer = new System.Timers.Timer();
//到达时间的时候执行事件;
aTimer.Elapsed += new ElapsedEventHandler(timer1_Tick);
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
aTimer.Interval = 60 * 1000;
//设置是执行一次(false)还是一直执行(true);
aTimer.AutoReset = true;
//是否执行System.Timers.Timer.Elapsed事件;
aTimer.Enabled = true;
}
[C#] 纯文本查看 复制代码
/// <summary>
/// 定时事件
/// </summary>
/// <param name="source">源对象</param>
/// <param name="e">ElapsedEventArgs事件对象</param>
protected void timer1_Tick(object source, ElapsedEventArgs e)
{
//帮助参考http://www.cnblogs.com/sufei/archive/2011/10/22/2221289.html
HttpHelps objhh = new HttpHelps();
//取网站源代码
string result = objhh.GetHttpRequestStringByNUll_Get("http://sufei.cnblogs.com", null);
//看看查询出来的网页代码是否存在Perky Su
if (result.Contains("Perky Su"))
{
//如果存在我设置的关键字说明我的网站是正常的
WriteFile("D:\\log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",
"您监控的网站http://sufei.cnblogs.com正常监控时间 " + DateTime.Now.ToString() + "\r\n");
}
else
{
WriteFile("D:\\log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",
"您监控的网站http://sufei.cnblogs.com出现异常情况监控时间 " + DateTime.Now.ToString() + "\r\n");
//如果不存在说明我的网站是不正常的
}
}
/// <summary>
/// 写文件
/// </summary>
/// <param name="_filePath">文件路径</param>
/// <param name="TOEXCELLR">要写入的内容</param>
private void WriteFile(string _filePath, string TOEXCELLR)
{
//检查是否创建文档成功
if (CreateXmlFile(_filePath))
{ //写文本,
using (StreamWriter fs = new StreamWriter(_filePath, true, System.Text.Encoding.UTF8))
{
fs.Write(TOEXCELLR);
}
}
}
/// <summary>
/// 创建文件 的方法
/// </summary>
/// <param name="filepath">路径</param>
/// <returns>文件存在返True否在为False</returns>
private static Boolean CreateXmlFile(string filepath)
{
try
{
//记录成功时的记录
if (!File.Exists(filepath))
{
using (StreamWriter xmlfs = new StreamWriter(filepath, true, System.Text.Encoding.UTF8))
{
xmlfs.Write("");
}
return true;
}
else
{
return true;
}
}
catch (Exception)
{
return false;
}
}
[C#] 纯文本查看 复制代码
[RunInstaller(true)]
public partial class InstallerServices : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
public InstallerServices()
{
// 该调用是设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
#region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// 创建ServiceProcessInstaller对象和ServiceInstaller对象
this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();
// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Username = null;
this.spInstaller.Password = null;
// 设定服务名称服务程序的名称
this.sInstaller.ServiceName = "Jinakong";
// 设定服务的启动方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });
}
#endregion
}