Saturday 19 July 2008

Cannot uninstall a .NET Windows Service with installutil /u when the service executable has been moved or deleted - Fix

There are a few perils unique to developing Windows Services in .NET. This is one of them.

The other day, I renamed some of my subversion working folders. Unfortunately, one of the folders that I renamed actually included a service that I had registered via installutil.exe on my local machine.

There is a problem with installutil.exe which means that this could be an unrecoverable Catch-22 situation. Here's why:

  1. You cannot uninstall it. If you try to uninstall it with installutil /u and point to your service (e.g. "uninstall /u DDK.ProjectName.MyNotificationServiceName", it cannot find the file will and give a "Exception occurred while initializing the installation: System.IO.FileNotFoundException: Could not load file or assembly '[Full Path To My File]' or one of its dependencies. The system cannot find the file specified.."

  2. You cannot install the exe to a different location with installutil because the service is already installed. If you do try to install it with a new path, you will just get the error "An exception occurred during the Install phase.System.ComponentModel.Win32Exception: The specified service already exists".

So to install a service with the same at the new location, I would have to:

  1. Copy the old file back to the original Windows Service Location location (or restore it from a backup) and run installutil /u on it. If I don't have the file anymore, I would not be able to do this.

  2. OR Remove the registry entry for the service.

This would not be as much of an issue if installutil /u recognized the missing service and prompted me if I wanted to remove the registry entry - but it didn't. I understand you want to do cleanup of a service when an uninstall is called - but you shouldn't be left in an unrecoverable state because of a lack of functionality in the core installer utility.

So when you don't have access to the file/drive you originally installed a service to, you can fix this unrecoverable (from the perspective of installutil) situation by either:

  1. Opening regedit

  2. Going to HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services

  3. Removing the old registry entry for your service.


Running "sc" from a command prompt - see screenshot below for parameters:


Pablo said...

Thanks for the info! I installed a custom vb.NET service successfully using "InstallUtil.exe". When I attempted to uninstall the service with "InstallUtil.exe", I received the "Windows could find the specified file" error even though I never changed the path or filename. Anyway, the "sc" utility worked great!

Unknown said...

Hi David, this is the second time that you saved me a lot of time.

Thank you very much!

PS: Send a hello from Brazil to AG for me :)

Anonymous said...

Hi,David,thank you very much.I installed VB.NET Service successfully using "InstallUtil.exe",by the way,I am come from China,and I know you been to the Great Wall,so did I.

Unknown said...

I just ran into this problem myself, and it turns out that uninstall will work if you run it from the new directory where your service is. If it can find the .exe file in the current directory, it will uninstall the service. It doesn't have to be in the original location.

lino said...

This solution to remove windows services works perfectly. Thank You.

Unknown said...

Tx David and Mike worked like a charm!

Unknown said...

Tx David and Mike worked like a charm!