1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using System.Collections;
- using System.ComponentModel;
- using System.Configuration.Install;
- using System.IO;
- using System.Security.Principal;
- using System.ServiceProcess;
- namespace PRSServices;
- public class PRSInstaller : Installer
- {
- private readonly ServiceProcessInstaller _proc;
- private readonly ServiceInstaller _svc;
- public PRSInstaller()
- {
- _proc = new ServiceProcessInstaller();
- _svc = new ServiceInstaller { StartType = ServiceStartMode.Automatic };
- Installers.Add(_svc);
- Installers.Add(_proc);
- }
- protected override void OnBeforeInstall(IDictionary savedState)
- {
- var username = Context.Parameters["/username"];
- if (string.IsNullOrWhiteSpace(username))
- {
- _proc.Account = ServiceAccount.LocalSystem;
- _proc.Username = WindowsIdentity.GetCurrent().Name;
- }
- else
- {
- _proc.Account = ServiceAccount.User;
- _proc.Username = username;
- _proc.Password = Context.Parameters["/password"] ?? "";
- }
- _svc.ServiceName = Context.Parameters["/name"];
- _svc.DisplayName = Context.Parameters["/displayName"];
- _svc.Description = Context.Parameters["/description"];
- // Change to .exe because .NET creates and runs .dll
- Context.Parameters["assemblypath"] =
- "\"" + Path.ChangeExtension(Context.Parameters["assemblypath"], "exe") + "\" /service=" + _svc.ServiceName;
- base.OnBeforeInstall(savedState);
- }
- protected override void OnBeforeUninstall(IDictionary savedState)
- {
- _svc.ServiceName = Context.Parameters["/name"];
- // Change to .exe because .NET creates and runs .dll
- Context.Parameters["assemblypath"] =
- "\"" + Path.ChangeExtension(Context.Parameters["assemblypath"], "exe") + "\" /service=" + _svc.ServiceName;
- base.OnBeforeUninstall(savedState);
- }
- }
|