【发布时间】:2016-08-01 22:34:54
【问题描述】:
我有以下代码可以停止和卸载服务。 它正确停止了服务,但是当我尝试卸载时它给了我这个错误:
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
代码:
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();
}
}
}
【问题讨论】: