using System;
using System.ServiceProcess;
using System.Configuration.Install;
using System.ComponentModel;
using System.IO;

public class SimpleService : ServiceBase

    public static void Main()
        ServiceBase.Run(new SimpleService());

    public SimpleService()
        CanPauseAndContinue = true;
        //Here we are setting a service name to show you don't need a custom name if you don't want one
        //This gets overriden in this example later      
        ServiceName = "SimpleService";

    protected override void OnStart(string[] args)
       //This is where your service would do something useful when it starts

    protected override void OnStop()
        //This is where your service would do something useful when it stops


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;
        // Service will have the following name (optional) this just shows
        // you don't need to have a custom name, you can omit this though
        serviceInstaller.ServiceName = "SimpleService";
        // Here we are going to hook up some custom events prior to the install and uninstall
        BeforeInstall += new InstallEventHandler(BeforeInstallEventHandler);
        BeforeUninstall += new InstallEventHandler(BeforeUninstallEventHandler);     
        // serviceInstaller.ServiceName = servicename;

     private void BeforeInstallEventHandler(object sender, InstallEventArgs e)
        // Add steps to perform any actions before the install process.
        Console.WriteLine("BeforeInstallEventHandler Called");
        Console.WriteLine("Please enter the name you would like this service installed as:");
        serviceInstaller.ServiceName = Console.ReadLine();
        Console.WriteLine("Attempting to install service as: " + serviceInstaller.ServiceName);      

    private void BeforeUninstallEventHandler(object sender, InstallEventArgs e)
        Console.WriteLine("BeforeUninstallEventHandler Called");
        serviceInstaller.ServiceName = RetrieveServiceName();
        // Add steps to perform any actions before the Uninstall process.
        Console.WriteLine("Code for BeforeUninstallEventHandler");    

    private void PersistServiceName(string serviceName)
        //This method stores the service name, you can choose whatever method to persist this data as you like
        TextWriter tw = new StreamWriter("Service.config", false);       

    private string RetrieveServiceName()
        string serviceName;
        //This method retrieves the service name, and should mirror the Perist method above
        TextReader tr = new StreamReader("Service.config");
        serviceName = tr.ReadLine();
        Console.WriteLine("ServiceName " + serviceName);
        return serviceName;