I have the following code that stops and un-installs a service. It stops the service correctly but it gives me this error when I try to un-install:
System.NullReferenceException: Object reference not set to an instance of an object. at System.Configuration.Install.Installer.Uninstall(IDictionary savedState) at System.ServiceProcess.ServiceInstaller.Uninstall(IDictionary savedState) at UpdateOrca.FuncoesUpdater.StopService(String serviceName, Int32 timeoutMilliseconds, Boolean Unninstall) in C:\Users\me\Downloads\UpdateOrcaV2013\UpdateOrca\UpdateOrca\FuncoesUpdater.cs:line 165 Code:
public void StopService(string serviceName, int timeoutMilliseconds, bool Unninstall) { if (ServicoInstalado(serviceName) == true) //&& ServiceRunning(serviceName) == true { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); ServiceInstaller ServiceInstallerObj = new ServiceInstaller(); if (Unninstall == true) { ServiceInstallerObj.ServiceName = serviceName; ServiceInstallerObj.Uninstall(null); (LINE OF THE ERROR) } } catch (Exception ex) { Program.Erro = ex.ToString(); Erro NewErro = new Erro(); NewErro.ShowDialog(); } } }