Click to See Complete Forum and Search --> : Windows services with C#


stardv
December 8th, 2004, 09:34 AM
I found some articles that describe how to create windows services with VB.Net. Is there any that shows how to do it with C#??

Thank you

darwen
December 8th, 2004, 10:10 AM
You should be able to translate the VB.NET code into C# easily enough.

If not, here's a little snippet I wrote ages back to test services :


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

class UserService : System.ServiceProcess.ServiceBase
{
public UserService()
{
this.AutoLog = true;
this.CanStop = true;
this.CanPauseAndContinue = false;
this.ServiceName = "TestService";
}

protected override void OnStart(string [] args)
{
}

protected override void OnStop()
{
}
}

class App
{
[STAThread]
static public void Main()
{
System.ServiceProcess.ServiceBase.Run(new UserService());
}
}

[RunInstallerAttribute(true)]
public class UserServiceInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;

public UserServiceInstaller()
{
serviceInstaller = new ServiceInstaller();
processInstaller = new ServiceProcessInstaller();

processInstaller.Account = ServiceAccount.LocalSystem;

serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "TestService";

Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}


Compile this up and run


InstallUtil <executable with '.exe'>


to install it into the services list, and


InstallUtil /u <executable with '.exe'>


to uninstall.

Darwen.

stardv
December 8th, 2004, 10:35 AM
Thanks again

You are very helpfull!

Wish you the best

:)