Search code examples
c#.netservice

How can I receive notification that my service is being uninstalled?


I would like to know if it is possible to receive notification that my service is being uninstalled?

I can receive notification that my service is being stopped.

    protected override void OnStop()
    {
        base.OnStop();
    }

but how can I tell if it's being uninstalled?


Solution

  • As it turns out custom parameters can be called from an installer using the Service Control (sc) command:

     sc control <name_of_service> 129
    

    Where 129 is a custom code that is handled by your service:

        protected enum customCommands
        {
            install = 128,
            uninstall = 129,
        }
    
        protected override void OnCustomCommand(int command)
        {
            base.OnCustomCommand(command);
    
            if (command >= 128 && command <= 255)
            {
                customCommands cust = (customCommands)command;
    
                switch (cust)
                {
                    case customCommands.install:
                        break;
    
                    case customCommands.uninstall:
                        break;
    
                    default:
                        break;
                }
            }
        }