public override DeploymentResult Execute() { var result = new DeploymentResult(); if (UserName.ShouldPrompt()) { UserName = _prompt.Prompt("Win Service '{0}' UserName".FormatWith(ServiceName)); } if (Password.ShouldPrompt()) { Password = _prompt.Prompt("Win Service '{0}' For User '{1}' Password".FormatWith(ServiceName, UserName)); } ServiceReturnCode returnCode = WmiService.Create(MachineName, ServiceName, ServiceDisplayName, ServiceLocation, StartMode, UserName, Password, Dependencies); if (returnCode != ServiceReturnCode.Success) { result.AddAlert("Create service returned {0}".FormatWith(returnCode.ToString())); } else { result.AddGood("Create service succeeded."); } return(result); }
private void InstallService(bool shouldInstall) { string exePath = txtPath.Text; if (string.IsNullOrEmpty(exePath)) { MessageBox.Show("Path to executable is required", "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); return; } ServiceReturnCode installStatus = ServiceReturnCode.NotSupported; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (s, dwe) => { if (shouldInstall) { installStatus = WmiService.Instance.Install(Controller.MachineName, _serviceName, _serviceName, exePath, ServiceUtils.ServiceStartMode.Automatic, "LocalSystem", null, null); } else { installStatus = WmiService.Instance.Uninstall(Controller.MachineName, _serviceName); } Thread.Sleep(5000); }; worker.RunWorkerCompleted += (s, rwe) => { busyIndicator.IsBusy = false; if (rwe.Error != null) { MessageBox.Show(rwe.Error.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); } else if (installStatus != ServiceReturnCode.Success) { MessageBox.Show(installStatus.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); } RefreshStatus(); }; busyIndicator.IsBusy = true; worker.RunWorkerAsync(); }