コード例 #1
0
        private void LoadServiceInfos()
        {
            #region GeneralTab

            //Set to readonly when it has already a service name
            if (!string.IsNullOrWhiteSpace(_tempServiceConfig.ServiceName))
            {
                TextBoxServiceName.Text = _tempServiceConfig.ServiceName;
            }

            if (!_createNewService)
            {
                TextBoxServiceName.IsReadOnly  = true;
                TextBoxServiceName.Foreground  = Brushes.Gray;
                TextBoxServiceName.BorderBrush = Brushes.LightGray;
            }

            TextBoxDisplayName.Text = _tempServiceConfig.DisplayName;

            if (!string.IsNullOrWhiteSpace(_tempServiceConfig.BinaryPath))
            {
                TextBoxFilePath.Text = _tempServiceConfig.BinaryPath;
            }

            TextBoxParam.Text       = _tempServiceConfig.Arguments;
            TextBoxDescription.Text = _tempServiceConfig.Description;

            //StartType
            switch (_tempServiceConfig.StartType)
            {
            case Advapi32.ServiceStartType.AutoStart:
                ComboBoxStartType.SelectedIndex = _tempServiceConfig.DelayedStart ? 1 : 0;
                break;

            case Advapi32.ServiceStartType.StartOnDemand:
                ComboBoxStartType.SelectedIndex = 2;
                break;

            case Advapi32.ServiceStartType.Disabled:
                ComboBoxStartType.SelectedIndex = 3;
                break;

            default:
                ComboBoxStartType.SelectedIndex = 0;
                break;
            }

            #endregion

            #region CustomUser

            if (Equals(_tempServiceConfig.Credentials, ServiceCredentials.LocalSystem))
            {
                TextBoxUsername.Clear();
                TextBoxPassword.Clear();
                CheckBoxUseLocalSystem.IsChecked    = true;
                CheckBoxUseVirtualAccount.IsChecked = false;
            }
            else if (ServiceCredentials.IsVirtualAccount(_tempServiceConfig.Credentials))
            {
                CheckBoxUseVirtualAccount.IsChecked = true;
                CheckBoxUseLocalSystem.IsChecked    = false;
            }
            else
            {
                TextBoxUsername.Text                = _tempServiceConfig.Credentials.Username;
                TextBoxPassword.Password            = _createNewService ? string.Empty : PLACEHOLDER_PASSWORD;
                CheckBoxUseLocalSystem.IsChecked    = false;
                CheckBoxUseVirtualAccount.IsChecked = false;
            }

            #endregion

            #region AdvancedTab


            TextBoxMaxRestarts.Text         = _tempServiceConfig.ProcessMaxRestarts.ToString();
            TextBoxProcessTimeoutTime.Text  = _tempServiceConfig.ProcessTimeoutTime.ToString();
            TextBoxProcessRestartDelay.Text = _tempServiceConfig.ProcessRestartDelay.ToString();
            TextBoxCounterResetTime.Text    = _tempServiceConfig.CounterResetTime.ToString();
            TextBoxLoadOrderGroup.Text      = _tempServiceConfig.LoadOrderGroup;

            //Process Priority
            switch (_tempServiceConfig.ProcessPriority)
            {
            case ProcessPriorityClass.Idle:
                ComboBoxProcessPriority.SelectedIndex = 0;
                break;

            case ProcessPriorityClass.BelowNormal:
                ComboBoxProcessPriority.SelectedIndex = 1;
                break;

            case ProcessPriorityClass.Normal:
                ComboBoxProcessPriority.SelectedIndex = 2;
                break;

            case ProcessPriorityClass.AboveNormal:
                ComboBoxProcessPriority.SelectedIndex = 3;
                break;

            case ProcessPriorityClass.High:
                ComboBoxProcessPriority.SelectedIndex = 4;
                break;

            case ProcessPriorityClass.RealTime:
                ComboBoxProcessPriority.SelectedIndex = 5;
                break;

            default:
                ComboBoxProcessPriority.SelectedIndex = 2;
                break;
            }

            CheckBoxIsConsoleApp.IsChecked    = _tempServiceConfig.IsConsoleApplication;
            RadioButtonUseCtrlC.IsChecked     = _tempServiceConfig.UseCtrlC;
            RadioButtonUseCtrlBreak.IsChecked = !_tempServiceConfig.UseCtrlC;


            //Hide check box interact with desktop on not supported systems (windows 10 1803+)
            if (!DaemonMasterUtils.IsSupportedWindows10VersionForIwd)
            {
                CheckBoxInteractDesk.IsChecked = false;
                CheckBoxInteractDesk.IsEnabled = false;
            }
            else
            {
                CheckBoxInteractDesk.IsChecked = _tempServiceConfig.CanInteractWithDesktop;
            }

            CheckBoxUseEventLog.IsChecked = _tempServiceConfig.UseEventLog;

            #endregion

            #region Dependency Listboxes

            #region DependOnService

            //Load Data into _dependOnServiceObservableCollection
            _dependOnServiceObservableCollection = new ObservableCollection <ServiceInfo>();
            foreach (string dep in _tempServiceConfig.DependOnService)
            {
                var serviceInfo = new ServiceInfo
                {
                    ServiceName = dep
                };

                //Get display name
                using (var serviceController = new ServiceController(dep))
                {
                    serviceInfo.DisplayName = serviceController.DisplayName;
                }

                _dependOnServiceObservableCollection.Add(serviceInfo);
            }

            //Sort the list in alphabetical order
            ICollectionView collectionView1 = CollectionViewSource.GetDefaultView(_dependOnServiceObservableCollection);
            collectionView1.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending));
            ListBoxDependOnService.ItemsSource = collectionView1;

            #endregion

            #region AllServices

            //Load Data into _allServicesObservableCollection
            _allServicesObservableCollection = new ObservableCollection <ServiceInfo>();
            foreach (ServiceController service in ServiceController.GetServices())
            {
                var serviceInfo = new ServiceInfo
                {
                    DisplayName = service.DisplayName,
                    ServiceName = service.ServiceName
                };

                if (_dependOnServiceObservableCollection.All(x => x.ServiceName != serviceInfo.ServiceName))
                {
                    _allServicesObservableCollection.Add(serviceInfo);
                }
            }

            //Sort the list in alphabetical order
            ICollectionView collectionView2 = CollectionViewSource.GetDefaultView(_allServicesObservableCollection);
            collectionView2.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending));
            ListBoxAllServices.ItemsSource = collectionView2;

            #endregion

            #region AllGroups

            //Load Data into _allGroupsObservableCollection
            _allGroupsObservableCollection = new ObservableCollection <string>(RegistryManagement.GetAllServiceGroups());
            //Sort the list in alphabetical order
            ICollectionView collectionView3 = CollectionViewSource.GetDefaultView(_allGroupsObservableCollection);
            collectionView3.SortDescriptions.Add(new SortDescription());
            ListBoxAllGroups.ItemsSource = collectionView3;

            #endregion

            #region DependOnGroup

            //Load Data into _dependOnGroupObservableCollection
            _dependOnGroupObservableCollection = new ObservableCollection <string>(_tempServiceConfig.DependOnGroup);
            //Sort the list in alphabetical order
            ICollectionView collectionView4 = CollectionViewSource.GetDefaultView(_dependOnGroupObservableCollection);
            collectionView3.SortDescriptions.Add(new SortDescription());
            ListBoxDependOnGroup.ItemsSource = collectionView4;

            #endregion

            #endregion
        }