Windows services registration

wengwashere

Newcomer
Joined
Jun 24, 2003
Messages
4
how do you register a windows service application via code (VB.NET) ?

i mean, we could register it through installutil in the command prompt, right? is there a way we could do this via code? if so, how?

thanks!
 
You may do it this way!!

I could install a service from my code. This is C# code, but you may "traslate" it to VB.NET:

ServiceInstaller serviceInstaller = new ServiceInstaller();
Hashtable stateSaver = new Hashtable();
InstallContext installContext = new InstallContext("ServiceInstall.log", new string[] {"assemblypath=" + Assembly.GetExecutingAssembly().Location});

serviceInstaller.Context = installContext;
try
{
serviceInstaller.Install(stateSaver);
serviceInstaller.Commit(stateSaver);
}
catch (Exception e)
{
Console.WriteLine("Exception while trying to install service: {0}", e);
serviceInstaller.Rollback(stateSaver);
}

// And here is the ServiceInstaller class:

[RunInstaller(true)]
public class ServiceInstaller : System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller;
private System.ServiceProcess.ServiceInstaller serviceInstaller;

private System.ComponentModel.Container components = null;

public ServiceInstaller()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();

// serviceProcessInstaller
this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller.Password = null;
this.serviceProcessInstaller.Username = null;

// serviceInstaller
this.serviceInstaller.DisplayName = "Service Name";
this.serviceInstaller.ServiceName = "Service_Name";
this.serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

// ServiceInstaller
this.Installers.AddRange(new System.Configuration.Install.Installer[] {this.serviceProcessInstaller, this.serviceInstaller});
}
}
 
Back
Top