Simple Window service

muralikrishnasurap-12704
Posted by muralikrishnasurap-12704 under C# category on | Points: 40 | Views : 1727
Hi,
Here i want to explain a simple window service which logs the data on text file for every start and stop operations of service.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Timers;

namespace PracticeService
{
public partial class Service1 : ServiceBase
{
FileStream fs = new FileStream(@"D:\Practive\Servicelogs\CabRequestServicelog.txt", FileMode.OpenOrCreate, FileAccess.Write);
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteLog("Cab Request Service started");
}
protected override void OnStop()
{
WriteLog("Cab Request Service Stopped");

}
public void WriteLog(string data)
{
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(data);
m_streamWriter.Flush();
m_streamWriter.Close();
}
}
}


So once window service is done using window service template in VSS 2008
no we need to deploy this service using intallutill.exe.

Command for installing Window service
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe D:\Applications\PracticeService\PracticeService\bin\Debug\PracticeService.exe


Command for Uninstalling Window service
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u D:\Applications\PracticeService\PracticeService\bin\Debug\PracticeService.exe

Comments or Responses

Posted by: T.saravanan on: 11/26/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi,

Kindly post your code inside the code tag.

Login to post response