private void OnUninstall(object sender, EventArgs e) { var result = ServiceInstaller.Uninstall(_infos.ServiceName); if (result.Success) { MessageBox.Show("Service uninstalled !", "Uninstall Service", MessageBoxButtons.OK, MessageBoxIcon.Information); if (File.Exists(_infoFile)) { File.Delete(_infoFile); } Close(); } else { MessageBox.Show(result.Error, "Uninstall Service", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void OnInstall(object sender, EventArgs e) { bool canInstall = IsValidServiceInfo(); if (canInstall) { var info = GetInfos(); var result = ServiceInstaller.Install(info.ServiceName, info.DisplayName, Utility.GetFilename(), info.ServiceType, info.ServiceAccount, info.ServicePassword, info.ServiceDescription); if (!result.Success) { MessageBox.Show(result.Error, "Install Service", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { string fname = Utility.GetInfoFile(); ServiceInstaller.SerializeObject(info, fname); MessageBox.Show("Service installed !", "Install Service", MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); } } }
private void OnInit(object sender, EventArgs e) { _infoFile = Utility.GetInfoFile(); if (File.Exists(_infoFile)) { _infos = ServiceInstaller.DeSerializeObject <ServiceInfos>(_infoFile); } else { _infos = new ServiceInfos(); _infos.ServiceType = ServiceInstaller.ServiceBootFlag.AutoStart; _infos.ServiceName = Utility.GetServiceName(); } tbServiceName.Text = _infos.ServiceName; bool isInstalled = ServiceInstaller.ServiceIsInstalled(tbServiceName.Text); bool isAdmin = Utility.IsAdministrator(); string message; if (isInstalled) { if (isAdmin) { message = "Service installed ...."; btnUninstall.Enabled = true; } else { message = "Service installed, but application not started as Administrator..."; btnUninstall.Enabled = false; } } else { message = "Service not installed ...."; btnUninstall.Enabled = false; } lbMessage.Text = message; }
private bool IsValidServiceInfo() { var infos = GetInfos(); if (string.IsNullOrWhiteSpace(infos.ServiceName)) { MessageBox.Show("Service Name empty !", "Install Service", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } bool isInstalled = ServiceInstaller.ServiceIsInstalled(infos.ServiceName); if (isInstalled) { MessageBox.Show("Service allready installed !", "Install Service", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } if (string.IsNullOrWhiteSpace(infos.DisplayName)) { MessageBox.Show("Display Name empty !", "Install Service", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } if (string.IsNullOrWhiteSpace(infos.ServiceDescription)) { MessageBox.Show("Display Description empty !", "Install Service", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } if (!Utility.IsAdministrator()) { MessageBox.Show("Service Installation is only possible, when application was started as Administrator !", "Install Service", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } return true; }