public IEnumerable<ServiceModel> GetConfiguredServices()
        {
            var serviceModels = new List<ServiceModel>();

            var reg = GetApplicationRegistryKey();
            if (reg != null)
            {
                var serviceNames = reg.GetValueNames();
                foreach (var serviceName in serviceNames)
                {
                    var valueKind = reg.GetValueKind(serviceName);
                    if (valueKind == RegistryValueKind.String)
                    {
                        var serviceDescription = reg.GetValue(serviceName);
                        var serviceModel = new ServiceModel
                            {
                                ServiceName = serviceName,
                                DisplayName = serviceDescription.ToString()
                            };
                        GetStatus(serviceModel);
                        serviceModels.Add(serviceModel);
                    }
                }
            }

            return serviceModels.OrderBy(x => x.DisplayName);
        }
        public void RemoveServiceReference(ServiceModel serviceModel)
        {
            var reg = GetApplicationRegistryKey();
            if (reg == null) return;

            var serviceNames = reg.GetValueNames();
            if (serviceNames.Contains(serviceModel.ServiceName))
                reg.DeleteValue(serviceModel.ServiceName);
        }
        public void AddServiceReference(ServiceModel serviceModel)
        {
            var reg = GetApplicationRegistryKey();
            if (reg == null) return;

            var serviceNames = reg.GetValueNames();
            if (serviceNames.Contains(serviceModel.ServiceName))
                return;

            reg.SetValue(serviceModel.ServiceName, serviceModel.DisplayName);
        }
Ejemplo n.º 4
0
 private string GetServiceStatusText(ServiceModel serviceModel)
 {
     switch (serviceModel.Status)
     {
         case ServiceControllerStatus.Paused:
             return "Paused";
         case ServiceControllerStatus.Running:
             return "Running";
         case ServiceControllerStatus.Stopped:
             return "Stopped";
         case ServiceControllerStatus.StartPending:
             return "Starting";
         case ServiceControllerStatus.PausePending:
             return "Pausing";
         case ServiceControllerStatus.StopPending:
             return "Stopping";
         default:
             return "Unknown";
     }
 }
Ejemplo n.º 5
0
        private void BuildFormRow(ServiceModel serviceModel, int labelNo, ref int startAllLeft, ref int stopAllLeft)
        {
            var removeButton = CreateButton(@"X", BtnRemoveService);
            removeButton.Top = TopMargin + ((ControlHeight + TopMargin) * labelNo);
            removeButton.Left = LeftMargin;
            removeButton.Width = ControlHeight;
            removeButton.Tag = serviceModel;

            var label = CreateLabel(serviceModel.DisplayName, "lblServiceName", labelNo);
            label.Top = TopMargin + ((ControlHeight + TopMargin) * labelNo);
            label.Left = removeButton.Left + removeButton.Width + LeftMargin;

            var runningLabel = CreateLabel(GetServiceStatusText(serviceModel), "lblRunning", labelNo);
            runningLabel.Top = label.Top;
            runningLabel.Left = label.Left + label.Width + LeftMargin;
            runningLabel.Width = RunningLabelWidth;
            runningLabel.Tag = serviceModel;

            var startButton = CreateButton(@"Start", BtnStartService);
            startButton.Top = runningLabel.Top;
            startButton.Left = runningLabel.Left + runningLabel.Width + LeftMargin;
            startButton.Tag = serviceModel;
            startAllLeft = startButton.Left;

            var stopButton = CreateButton(@"Stop", BtnStopService);
            stopButton.Top = runningLabel.Top;
            stopButton.Left = startButton.Left + startButton.Width + LeftMargin;
            stopButton.Tag = serviceModel;
            stopAllLeft = stopButton.Left;
        }
        public void StopService(ServiceModel serviceModel)
        {
            var sc = new ServiceController(serviceModel.ServiceName);
            if ((sc.Status == ServiceControllerStatus.Running) || (sc.Status == ServiceControllerStatus.Paused))
            {
                sc.Stop();
                sc.WaitForStatus(ServiceControllerStatus.Stopped);
            }

            serviceModel.Status = sc.Status;
        }
        public void GetStatus(ServiceModel serviceModel)
        {
            var sc = new ServiceController(serviceModel.ServiceName);

            serviceModel.Status = sc.Status;
        }