CEL_Payroll/Ease.SvcHost/ServiceInstaller.cs

68 lines
2.2 KiB
C#
Raw Permalink Normal View History

2024-09-17 14:30:13 +06:00
using System;
using Microsoft.Win32;
using System.Collections;
using System.ComponentModel;
using System.ServiceProcess;
using System.Configuration.Install;
namespace Ease.SvcHost
{
/// <summary>
/// Summary description for Installer.
/// </summary>
[RunInstaller(true), ToolboxItem(false)]
public class HostServiceInstaller : Installer
{
private ServiceInstaller _serviceInstaller;
public HostServiceInstaller()
{
_serviceInstaller = new ServiceInstaller();
_serviceInstaller.StartType = ServiceStartMode.Automatic;
ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
Installers.AddRange(new System.Configuration.Install.Installer[] { _serviceInstaller, processInstaller });
}
private string GetServiceName(string name)
{
return string.Format("Ease.SvcHost{0}{1}", (name.Length > 0 ? "_" : ""), name.Replace(" ", "_")).Trim();
}
public override void Install(IDictionary stateSaver)
{
string name = string.Empty, desc = string.Empty;
if (Context.Parameters.ContainsKey("name"))
name = Context.Parameters["name"];
if (Context.Parameters.ContainsKey("desc"))
desc = Context.Parameters["desc"];
_serviceInstaller.DisplayName = string.Format("Ease.SvcHost{0} {1}", (name.Length > 0 ? ":" : ""), name).Trim();
_serviceInstaller.ServiceName = this.GetServiceName(name);
base.Install(stateSaver);
RegistryKey key = RegistryHelper.GetServiceKey(_serviceInstaller.ServiceName);
key.SetValue("ImagePath", (string)key.GetValue("ImagePath") + " -start " + _serviceInstaller.ServiceName);
key.SetValue("Description", desc.Length == 0 ? "Ease Service Host" : desc);
key.Close();
}
public override void Uninstall(IDictionary savedState)
{
string name = string.Empty;
if (Context.Parameters.ContainsKey("name"))
name = Context.Parameters["name"];
_serviceInstaller.ServiceName = this.GetServiceName(name);
base.Uninstall(savedState);
}
}
}