Пример #1
0
        public static void Uninstall(string ServiceName)
        {
            IntPtr intPtr = ServiceInstaller.OpenSCManager(ServiceManagerRights.Connect);

            try
            {
                IntPtr intPtr1 = ServiceInstaller.OpenService(intPtr, ServiceName, ServiceRights.QueryStatus | ServiceRights.Stop | ServiceRights.Delete | ServiceRights.StandardRightsRequired);
                if (intPtr1 == IntPtr.Zero)
                {
                    throw new ApplicationException("Service not installed.");
                }
                try
                {
                    ServiceInstaller.StopService(intPtr1);
                    if (ServiceInstaller.DeleteService(intPtr1) == 0)
                    {
                        int lastWin32Error = Marshal.GetLastWin32Error();
                        throw new ApplicationException(string.Concat("Could not delete service ", lastWin32Error));
                    }
                }
                finally
                {
                    ServiceInstaller.CloseServiceHandle(intPtr1);
                }
            }
            finally
            {
                ServiceInstaller.CloseServiceHandle(intPtr);
            }
        }
Пример #2
0
        public static void StopService(string Name)
        {
            IntPtr intPtr = ServiceInstaller.OpenSCManager(ServiceManagerRights.Connect);

            try
            {
                IntPtr intPtr1 = ServiceInstaller.OpenService(intPtr, Name, ServiceRights.QueryStatus | ServiceRights.Stop);
                if (intPtr1 == IntPtr.Zero)
                {
                    throw new ApplicationException("Could not open service.");
                }
                try
                {
                    ServiceInstaller.StopService(intPtr1);
                }
                finally
                {
                    ServiceInstaller.CloseServiceHandle(intPtr1);
                }
            }
            finally
            {
                ServiceInstaller.CloseServiceHandle(intPtr);
            }
        }