Файл D:\WindowsService1\ProjectInstaller.Designer.cs
...
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; // my line 1
...
this.serviceInstaller1.Description = "My First Service demo"; // my line 2
...
this.serviceInstaller1.DisplayName = "MyFirstService.Demo"; // my line 3
...
Файл D:\WindowsService1\Service1.cs
using System;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
private Timer _timer = new Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteToFile("Service is started at " + DateTime.Now);
_timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
_timer.Interval = 5000; //5 seconds
_timer.Enabled = true;
}
protected override void OnStop()
{
WriteToFile("Service is stopped at " + DateTime.Now);
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
WriteToFile("Service is recall at " + DateTime.Now);
}
public void WriteToFile(string Message)
{
string filepath = "D:\\my_service.txt";
// will write text in end of file
using (StreamWriter fileStream = File.Exists(filepath) ? File.AppendText(filepath): File.CreateText(filepath))
{
fileStream.WriteLine(Message);
}
}
}
}
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe D:\WindowsService1\bin\Debug\WindowsService1.exe
net start MyFirstService.Demo
net stop MyFirstService.Demo
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u D:\WindowsService1\bin\Debug\WindowsService1.exe