Example #1
0
        public static void DeleteService(string serviceName)
        {
            var databaseHandle = ServiceUtil.OpenServiceDatabase();

            try { ServiceUtil.DeleteService(databaseHandle, serviceName); }
            finally { SafeNativeMethods.CloseServiceHandle(databaseHandle); }

            // Not sure why this comes after the deletion, but .NET ServiceInstaller does this, so perhaps there's a good reason
            ServiceUtil.StopService(serviceName);
        }
Example #2
0
        /// <summary>
        /// Uninstalls all services in this process (i.e. unregisters them from the service manager). The current
        /// state of the services doesn't matter for this method - running services will be stopped first.
        /// </summary>
        public void Uninstall()
        {
            var databaseHandle = ServiceUtil.OpenServiceDatabase();

            try
            {
                foreach (var service in Services)
                {
                    ServiceUtil.DeleteService(databaseHandle, service.ServiceName);
                    ServiceUtil.StopService(service.ServiceName); // Not sure why this comes after the deletion, but .NET ServiceInstaller does this, so perhaps there's a good reason
                }
            }
            finally
            {
                ServiceUtil.CloseServiceDatabase(databaseHandle);
            }
        }
Example #3
0
        private void install(string user, string password, string exeArgs)
        {
            if (Services.Count == 0)
            {
                throw new InvalidOperationException("There are no services defined.");
            }

            // user is null for the local system account
            if (user != null && !user.Contains('\\'))
            {
                user = "******" + user;
            }

            string binaryPathAndArgs = Assembly.GetEntryAssembly().Location;

            if (binaryPathAndArgs == null || binaryPathAndArgs.Length == 0)
            {
                throw new InvalidOperationException("Could not retrieve entry assembly file name.");
            }
            binaryPathAndArgs = "\"" + binaryPathAndArgs + "\"";
            if (!string.IsNullOrEmpty(exeArgs))
            {
                binaryPathAndArgs += " " + exeArgs;
            }

            IntPtr databaseHandle = ServiceUtil.OpenServiceDatabase();

            try
            {
                foreach (var service in Services)
                {
                    ServiceUtil.InstallService(databaseHandle, Services.Count, service.ServiceName, service.ServiceDisplayName, service.ServiceDescription, service.ServiceStartMode, service.ServicesDependedOn, binaryPathAndArgs, user, password);
                }
            }
            finally
            {
                ServiceUtil.CloseServiceDatabase(databaseHandle);
            }
        }