Correct way to uninstall a Windows service?

Mun picture Mun · Aug 8, 2010 · Viewed 7.3k times · Source

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.

Answer

Mun picture Mun · Jan 28, 2011

For the benefit of anyone looking for an answer to these problems:

Service is not stopped prior to uninstalling

Haven't yet found a solution to this.

Installation folder not deleted after uninstalling

The OnAfterUninstall method in the project installer needs to be overridden, and any created files must be deleted. The application installer folder is automatically deleted if it doesn't contain any files after this step.

protected override void OnAfterUninstall(IDictionary savedState)
{
    base.OnAfterUninstall(savedState);

    string targetDir = Context.Parameters["TargetDir"]; // Must be passed in as a parameter

    if (targetDir.EndsWith("|"))
        targetDir = targetDir.Substring(0, targetDir.Length-1);

    if (!targetDir.EndsWith("\\"))
        targetDir += "\\";

    if (!Directory.Exists(targetDir))
    {
        Debug.WriteLine("Target dir does not exist: " + targetDir);
        return;
    }

    string[] files = new[] { "File1.txt", "File2.tmp", "File3.doc" };
    string[] dirs  = new[] { "Logs", "Temp" };

    foreach (string f in files)
    {
        string path = Path.Combine(targetDir, f);

        if (File.Exists(path))
            File.Delete(path);
    }

    foreach (string d in dirs)
    {
        string path = Path.Combine(targetDir, d);

        if (Directory.Exists(d))
            Directory.Delete(d, true);
    }

    // At this point, all generated files and directories must be deleted.
    // The installation folder will be removed automatically.
}

Remember, the installation folder must be passed in as a parameter:

  • Right click on your setup project, then choose View -> Custom Actions
  • The custom actions will open in your main window. Right click on "Primary output from XXX" under the Uninstall node and select "Properties Window"
  • In the properties window, under CustomActionData, enter the following: /TargetDir="[TARGETDIR]|" (note the pipe at the end, do not remove this).

This will pass the installation folder as a parameter to your uninstall routine so that you know where the application was installed and can delete your generated files and folders.