Windows Services in Visual Studio 2005 Standard Edition


Overview

To create a Windows Service in Visual Studio, typical instructions start with creating a Windows Service project. However, this project type is not available in Visual Studio 2005 Standard Edition. This articles is a walkthrough of creating a Windows Service in Visual Studio 2005 Standard Edition.

Walkthrough

Creating the Project and Objects



Image


Image



Image


Image

Coding the Service


{copytext|ProgramCs}
class Program
{
    static void Main()
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun
            = new System.ServiceProcess.ServiceBase[] { new WeatherWidgetService() };

        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
} 


{copytext|WeatherWidgetServiceCs}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace Weather_Widget_Service
{
    partial class WeatherWidgetService : ServiceBase
    {
        public WeatherWidgetService()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            WriteLog("Service started");
        }
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop 
            // your service.
            WriteLog("Service stopped");
        }
        private void WriteLog(string msg)
        {
            msg = "\n" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + ">" + msg;
            File.AppendAllText(@"C:\Data\WeatherService.log", msg);
        }
    }
} 

Deploying and Testing the Service