private static void GetAccountFields(ServiceAccountDescriptor accountDescriptor, out string accountName, out string password) { switch (accountDescriptor.AccountType) { case ServiceAccount.LocalSystem: accountName = null; password = null; break; case ServiceAccount.LocalService: accountName = @"NT AUTHORITY\LocalService"; password = string.Empty; break; case ServiceAccount.NetworkService: accountName = @"NT AUTHORITY\NetworkService"; password = string.Empty; break; case ServiceAccount.User: accountName = accountDescriptor.AccountName.IndexOf('\\') >= 0 ? accountDescriptor.AccountName : ".\\" + accountDescriptor.AccountName; password = accountDescriptor.Password; break; default: throw new InvalidEnumArgumentException(); } }
public static void InstallService(System.Configuration.Install.Installer installer) { System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller; System.ServiceProcess.ServiceInstaller serviceInstaller; FindInstallers(installer, out serviceProcessInstaller, out serviceInstaller); ServiceAccountDescriptor accountDescriptor = new ServiceAccountDescriptor(serviceProcessInstaller); InstallService(serviceInstaller.ServiceName, serviceInstaller.DisplayName, serviceInstaller.StartType, accountDescriptor); }
public static void InstallService(string serviceName, string displayName, ServiceStartMode startMode, ServiceAccountDescriptor accountDescriptor) { AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation; string servicePath = Path.Combine(setup.ApplicationBase, setup.ApplicationName); InstallService(serviceName, displayName, servicePath, startMode, accountDescriptor); }
public static void InstallService(string serviceName, string displayName, string servicePath, ServiceStartMode startMode, ServiceAccountDescriptor accountDescriptor) { uint startModeFlag = GetSratModeFlag(startMode); string accountName, password; GetAccountFields(accountDescriptor, out accountName, out password); if (accountDescriptor.AccountType == ServiceAccount.User) { AccountRights.SetRights(accountDescriptor.AccountName, "SeServiceLogonRight", true); } IntPtr hSCMHandle = IntPtr.Zero, hServHandle = IntPtr.Zero; try { hSCMHandle = OpenSCManager(null, null, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE); if (hSCMHandle == IntPtr.Zero) { throw new Win32Exception(); } hServHandle = CreateService(hSCMHandle, serviceName, displayName, SC_MANAGER_CREATE_SERVICE, SERVICE_WIN32_OWN_PROCESS, startModeFlag, SERVICE_ERROR_NORMAL, servicePath, null, UIntPtr.Zero, null, accountName, password); if (hServHandle == IntPtr.Zero) { throw new Win32Exception(); } } finally { if (hServHandle != IntPtr.Zero) { CloseServiceHandle(hServHandle); } if (hSCMHandle != IntPtr.Zero) { CloseServiceHandle(hSCMHandle); } } }
public static void InstallService(System.Configuration.Install.Installer installer) { System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller; System.ServiceProcess.ServiceInstaller serviceInstaller; FindInstallers(installer, out serviceProcessInstaller, out serviceInstaller); ServiceAccountDescriptor accountDescriptor = new ServiceAccountDescriptor(serviceProcessInstaller); InstallService(serviceInstaller.ServiceName, serviceInstaller.DisplayName, serviceInstaller.StartType, accountDescriptor); }
public static void InstallService(string serviceName, string displayName, ServiceStartMode startMode, ServiceAccountDescriptor accountDescriptor) { string servicePath = Assembly.GetExecutingAssembly().Location; InstallService(serviceName, displayName, servicePath, startMode, accountDescriptor); }
public static void InstallService(string serviceName, string displayName, string servicePath, ServiceStartMode startMode, ServiceAccountDescriptor accountDescriptor) { uint startModeFlag = GetSratModeFlag(startMode); string accountName, password; GetAccountFields(accountDescriptor, out accountName, out password); if (accountDescriptor.AccountType == ServiceAccount.User) AccountRights.SetRights(accountDescriptor.AccountName, "SeServiceLogonRight", true); IntPtr hSCMHandle = IntPtr.Zero, hServHandle = IntPtr.Zero; try { hSCMHandle = OpenSCManager(null, null, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE); if (hSCMHandle == IntPtr.Zero) throw new Win32Exception(); hServHandle = CreateService(hSCMHandle, serviceName, displayName, SC_MANAGER_CREATE_SERVICE, SERVICE_WIN32_OWN_PROCESS, startModeFlag, SERVICE_ERROR_NORMAL, servicePath, null, UIntPtr.Zero, null, accountName, password); if (hServHandle == IntPtr.Zero) throw new Win32Exception(); } finally { if (hServHandle != IntPtr.Zero) CloseServiceHandle(hServHandle); if (hSCMHandle != IntPtr.Zero) CloseServiceHandle(hSCMHandle); } }
private static void GetAccountFields(ServiceAccountDescriptor accountDescriptor, out string accountName, out string password) { switch (accountDescriptor.AccountType) { case ServiceAccount.LocalSystem: accountName = null; password = null; break; case ServiceAccount.LocalService: accountName = @"NT AUTHORITY\LocalService"; password = string.Empty; break; case ServiceAccount.NetworkService: accountName = @"NT AUTHORITY\NetworkService"; password = string.Empty; break; case ServiceAccount.User: accountName = accountDescriptor.AccountName.IndexOf('\\') >= 0 ? accountDescriptor.AccountName : ".\\" + accountDescriptor.AccountName; password = accountDescriptor.Password; break; default: throw new InvalidEnumArgumentException(); } }
public static void InstallService(string serviceName, string displayName, ServiceStartMode startMode, ServiceAccountDescriptor accountDescriptor) { string servicePath = Assembly.GetExecutingAssembly().Location; InstallService(serviceName, displayName, servicePath, startMode, accountDescriptor); }