private static bool InstallService() { bool success = false; try { string exeFullPath = System.Reflection.Assembly.GetExecutingAssembly().Location; string workingPath = System.IO.Path.GetDirectoryName(exeFullPath); string logPath = System.IO.Path.Combine(workingPath, "Install.log"); ServiceStartMode startmode = ServiceStartMode.Automatic; ServiceAccount account = ServiceAccount.LocalService; bool DelayedAutoStart = false; string username = ""; string password = ""; InstallerForm installerForm = new InstallerForm(); installerForm.StartType = ServiceStartMode.Automatic; installerForm.AccountType = ServiceAccount.User; installerForm.BringToFront(); installerForm.TopMost = true; if (installerForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { startmode = installerForm.StartType; account = installerForm.AccountType; if (installerForm.AccountType == ServiceAccount.User) { username = installerForm.UserName; password = installerForm.Password; } DelayedAutoStart = installerForm.DelayedStart; } Hashtable savedState = new Hashtable(); ProjectInstaller myProjectInstaller = new ProjectInstaller(true); InstallContext myInstallContext = new InstallContext(logPath, new string[] { }); myProjectInstaller.Context = myInstallContext; myProjectInstaller.ServiceName = "QuickMon 3 Service"; myProjectInstaller.DisplayName = "QuickMon 3 Service"; myProjectInstaller.Description = "QuickMon 3 Monitoring, alerting and Remote Service"; myProjectInstaller.StartType = startmode; myProjectInstaller.DelayedAutoStart = DelayedAutoStart; myProjectInstaller.Account = account; if (account == ServiceAccount.User) { myProjectInstaller.ServiceUsername = username; myProjectInstaller.ServicePassword = password; } myProjectInstaller.Context.Parameters["AssemblyPath"] = exeFullPath; myProjectInstaller.Install(savedState); success = true; } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "Install service", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } return(success); }
public static bool InstallService(string serviceExePath, string serviceName, string displayName, string description, string serviceParameters) { bool success = false; try { string workingPath = System.IO.Path.GetDirectoryName(serviceExePath); string logPath = System.IO.Path.Combine(workingPath, "Install.log"); ServiceStartMode startmode = ServiceStartMode.Automatic; ServiceAccount account = ServiceAccount.LocalService; string username = ""; string password = ""; bool delayedStart = true; InstallerForm installerForm = new InstallerForm(); installerForm.StartType = ServiceStartMode.Automatic; installerForm.AccountType = ServiceAccount.User; installerForm.BringToFront(); installerForm.TopMost = true; if (installerForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { startmode = installerForm.StartType; account = installerForm.AccountType; delayedStart = installerForm.DelayedStart; if (installerForm.AccountType == ServiceAccount.User) { username = installerForm.UserName; password = installerForm.Password; } Hashtable savedState = new Hashtable(); ProjectInstallerForHelper myProjectInstaller = new ProjectInstallerForHelper(delayedStart); InstallContext myInstallContext = new InstallContext(logPath, new string[] { }); myProjectInstaller.Context = myInstallContext; myProjectInstaller.ServiceName = serviceName; myProjectInstaller.DisplayName = displayName; myProjectInstaller.Description = description; myProjectInstaller.StartType = startmode; myProjectInstaller.Account = account; myProjectInstaller.DelayedAutoStart = installerForm.DelayedStart; if (account == ServiceAccount.User) { myProjectInstaller.ServiceUsername = username; myProjectInstaller.ServicePassword = password; } myProjectInstaller.Context.Parameters["AssemblyPath"] = serviceExePath + " " + serviceParameters; myProjectInstaller.Install(savedState); success = true; } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "Install service", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } return(success); }