views:

49

answers:

1

I've got a windows service, built using C#, that is installed via a VS2008 setup project, and am having a couple of problems occurring with the uninstall process:

Service is not stopped prior to uninstalling

When the uninstall routine runs, it throws up an error about files being in use. Clicking continue completes the installer correctly, but the service still shows up in the list, so it's not being uninstalled properly.

(At present, I have to resort to deleting it manually using sc delete servicename).

I'm trying to stop the service before uninstalling using the following code, but it doesn't seem to be taking effect:

protected override void OnBeforeUninstall(IDictionary savedState)
{
   base.OnBeforeUninstall(savedState);
   ServiceController serviceController = new ServiceController(MyInstaller.ServiceName);
   serviceController.Stop();
}

When is this code called, and how can I stop the service prior to uninstalling?

Installation folder not deleted after uninstalling

The application also creates some files within it's installation folder when executed. After uninstalling, the installation folder (C:\Program Files\MyApp) is not deleted, and contains the files created by the application, though all other files that were actually installed by the installer have been deleted successfully.

Is it possible for the uninstall process to delete the installation folder, including all generated files within that folder, and if so, how?

Thanks.

A: 

Most likely the service is just taking a little time to shut down, and you're continuing on before the service has fully stopped. Try calling the WaitForStatus(ServiceControllerStatus) method.

This will cause your code to wait until the service processes the "stop" message, and shuts down. Once the service is actually shut down, it will no longer be holding on to any file handles.

Nader Shirazie
I've tried this, without any luck. Some further debugging reveals that none of the uninstall (OnBeforeUninstall, OnAfterUninstall, Uninstall) methods are actually being called before Windows pops up the "Files in use" warning, which would be why the service is not being stopped.
Mun
Ah, ok. A different question, when you say "files created by the application", you mean files not present when the installer completes the installation, rather created as the application runs? Those files may need to be cleaned up manually (onafterinstall?)... many installers will be very careful about what they "automatically" delete.
Nader Shirazie
Yep, I managed to resolve that problem by passing in the installation directory as a custom parameter, and overriding the OnAfterUninstall method to delete the files created by the application. If the folder is empty after that, the installer automatically removes it.
Mun