namespace Microshaoft { using System; using System.Timers; using System.Management; using System.ServiceProcess; using System.Diagnostics; using System.ComponentModel; using System.Collections.Generic; using System.Security.Principal; using System.Configuration.Install; using Microshaoft; public class SimpleService : ServiceBase //继承于 ServiceBase { public const string serviceName = "ServicesDaemon"; private Timer _timer; public static void Main(string[] args) { SimpleService x = new SimpleService(); int l = 0; if (args != null) { l = args.Length; } if (l > 0) //有参数时以 console 方式运行 { Console.Title = "serviceName"; Console.WriteLine("Run as Console"); x.OnStart(null); Console.ReadLine(); } else //intallutil 成服务后 //即: 无参数时,以 Service 方式运行 { Console.WriteLine("Run as Service"); ServiceBase.Run(x); } } public SimpleService() { CanPauseAndContinue = true; ServiceName = SimpleService.serviceName; } protected override void OnStart(string[] args) { Console.WriteLine(".Net Version: {0}", Environment.Version.ToString()); Console.WriteLine("Current Identity: {0}", WindowsIdentity.GetCurrent().Name); Console.WriteLine("{0} started,at {1}", SimpleService.serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ss")); _timer = new Timer(); _timer.Interval = 60 * 1000; _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed); _timer.Start(); _timer_Elapsed(null, null); //在这里写你的程序 } void _timer_Elapsed(object sender, ElapsedEventArgs e) { _timer.Stop(); try { Process(); } catch { } finally { _timer.Start(); } } private void Process() { ManagementObjectSearcher searcher = new ManagementObjectSearcher ( "root\\CIMV2", "SELECT * FROM Win32_Service where name like 'hello%'" ); ManagementObjectCollection moc = searcher.Get(); foreach (ManagementObject mo in moc) { string logMessage; string service = null; try { service = mo["Name"].ToString(); if (mo["State"].ToString().ToLower() != "running") { logMessage = string.Format ( "主机[{0}],[{1}],[{2}] 服务于[{3}]没有运行" , Environment.MachineName , service , mo["StartMode"].ToString() , DateTime.Now ); EventLogHelper.WriteEventLogEntry ( this.ServiceName + " log", service + " source", logMessage, EventLogEntryType.Error ); if (mo["StartMode"].ToString().ToLower() == "auto") { ServiceController controller = new ServiceController(service); if (controller.Status != ServiceControllerStatus.Running) { controller.Start(); } logMessage = string.Format ( "主机[{0}],[{1}],[{2}] 服务于[{3}]成功重新启动" , Environment.MachineName , service , mo["StartMode"].ToString() , DateTime.Now ); EventLogHelper.WriteEventLogEntry ( this.ServiceName + " log", service + " source", logMessage, EventLogEntryType.Information ); } } } catch (ManagementException me) { logMessage = string.Format ( "主机[{0}],[{1}],[{2}] 服务查询发生[{3}]异常[{4}],于[{5}]" , Environment.MachineName , service , mo["StartMode"].ToString() , "ManagementException" , me.ToString() , DateTime.Now ); EventLogHelper.WriteEventLogEntry ( this.ServiceName + " log", service + " source", logMessage, EventLogEntryType.Error ); } catch (Exception e) { logMessage = string.Format ( "主机[{0}],[{1}],[{2}] 服务查询发生[{3}]异常[{4}],于[{5}]" , Environment.MachineName , service , mo["StartMode"].ToString() , "Exception" , e.ToString() , DateTime.Now ); EventLogHelper.WriteEventLogEntry ( this.ServiceName + " log", service + " source", logMessage, EventLogEntryType.Error ); } } } } //以下就是比普通应用程序多出的 ProjectInstaller [RunInstallerAttribute(true)] public class ProjectInstaller : Installer { private ServiceInstaller serviceInstaller; private ServiceProcessInstaller processInstaller; public ProjectInstaller() { processInstaller = new ServiceProcessInstaller(); serviceInstaller = new ServiceInstaller(); // Service will run under system account processInstaller.Account = ServiceAccount.LocalSystem; // Service will have Start Type of Manual serviceInstaller.StartType = ServiceStartMode.Manual; serviceInstaller.ServiceName = SimpleService.serviceName; Installers.Add(serviceInstaller); Installers.Add(processInstaller); } } } namespace Microshaoft { using System; using System.Diagnostics; class EventLogHelper { public static void WriteEventLogEntry ( string logName, string sourceName, string logMessage, EventLogEntryType logEntryType ) { if (!EventLog.SourceExists(sourceName)) { EventLog.CreateEventSource(sourceName, logName); } EventLog log = new EventLog(); log.Source = sourceName; log.WriteEntry(logMessage, logEntryType); } } }