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});
}
}