Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Sunday, July 25, 2010

How to create a windows service

Free Microsoft MCTS 70-536 Examination Training and Preparation for Jobs





Window services are applications which run in background with no user interaction.We can create these services for many purposes like tracking the changes done to the files in a particular folder for example.In our example, we will be creating a windows service, which will create a text document at certain location on our local computer and writes the status of our service along with the time stamp.



Below is the code for the same.

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;

namespace WsEg1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"C:\My stuff\Work Stuff\Pdf\ServiceInfo.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Service has been started at " + System.DateTime.Now);
sw.Close();
fs.Close();
}

protected override void OnStop()
{
FileStream fs = new FileStream(@"C:\My stuff\Work Stuff\Pdf\ServiceInfo.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Service has been stopped at " + System.DateTime.Now);
sw.Close();
fs.Close();
}
}
}

After creating windows service we will add an installer to the project which will have the configuration information of our windows service.To know how you can an installer to a windows service, please check the video.
Regards
Sameer Shaik

Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test

No comments:

Post a Comment