16 August 2012

What To Do When Windows Service Will Not Delete


When a service is removed from any Microsoft Windows operating system, to date, the system must be rebooted, to completely remove the service.  After restarting the operating system, the service entry should be deleted.  If the service still appears in the Services MMC, or a "specified service already exists" error is displayed upon installing a fresh copy of the service, you'll probably need to tweak the registry, to manually remove the service key.

BE VERY CAREFUL, WHEN EDITING THE SYSTEM REGISTRY!

  1. Execute regedit.exe
    1. Windows Vista/2008 and newer: type "regedit" in the start menu search box
    2. Older versions: click the "Run" option, located in the start menu
  2. Press the Enter key, to launch
  3. Back up your registry
    1. Click file menus: File > Export...
    2. Select a location to save the registry backup file
    3. Click the Save button
  4. Using the tree menu, navigate to: HKEY_LOCAL_MACHINE > SYSTEM> CurrentControlSet > services
  5. Locate the child node of "services" that matches the service you need to delete
  6. Press the delete key on the keyboard -- a delete confirmation appears
  7. Click the Yes button, in the delete confirmation -- the registry key is deleted
  8. Close the registry editor

How To Work Around "Specified Service Already Exists"

If you have ever created a Windows Service, you are likely familiar with the "specified service already exists" installation error message.  Most developers encounter this situation after making code changes, and attempting to install the updated service, for testing.  The problem is that Windows services can not be removed without a system reboot.

The solution is simple.  Install the service for testing, only once.  To test newly-compiled changes to the service:

  1. Stop the service
  2. Copy new files into the service installation directory, overwriting the old ones
  3. Start the service

Easy!

As always, use unit tests, to test code, instead of testing through execution.  There is little excuse to not test code, these days.  To expedite testing and TDD, I suggest installing NCrunch for Visual Studio (http://www.ncrunch.net/).