/// <summary> /// Regenerates the contents of the services list view. /// </summary> private void RefreshList() { // Store the machine used to browse services so we can compare it with the current value in newMachine to know if we need to call RefreshList. ServicesListView.Tag = NewMachine; ServicesListView.Items.Clear(); if (NewMachine == null || !NewMachine.IsOnline) { return; } Cursor = Cursors.WaitCursor; try { string currentFilter = FilterCheckBox.Checked ? Settings.Default.AutoAddPattern.Trim() : FilterTextBox.Text.ToLower(); ServicesListView.BeginUpdate(); List <ManagementObject> services = new List <ManagementObject>(); if (NewMachine != null && MachineLocationType == Machine.LocationType.Remote) { ManagementObjectCollection machineServicesCollection = NewMachine.GetWmiServices(true); if (machineServicesCollection != null) { services.AddRange(machineServicesCollection.Cast <ManagementObject>()); } } else { services = Service.GetInstances(string.Empty); } services = services.OrderBy(x => x.Properties["DisplayName"].Value).ToList(); if (!string.IsNullOrEmpty(currentFilter)) { services = services.Where(f => f.Properties["DisplayName"].Value.ToString().ToLowerInvariant().Contains(currentFilter)) .ToList(); } foreach (ManagementObject item in services) { string serviceName = item.Properties["Name"].Value.ToString(); var displayName = item.Properties["DisplayName"].Value; var newItem = new ListViewItem { Text = displayName != null?displayName.ToString() : serviceName, Tag = serviceName }; newItem.SubItems.Add(item.Properties["State"].Value.ToString()); ServicesListView.Items.Add(newItem); } } finally { ServicesListView.EndUpdate(); Cursor = Cursors.Default; } }
/// <summary> /// Get all services and their status /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void GetServicesAsJsonButton_Click(object sender, EventArgs e) { ServicesListView.Items.Clear(); var serviceItems = await PowerShellOperations.GetServicesAsJson(); ServiceCountLabel.Text = serviceItems.Count.ToString(); ServicesListView.BeginUpdate(); try { foreach (var serviceItem in serviceItems) { ServicesListView.Items.Add(new ListViewItem(serviceItem.ItemArray())); } ServicesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); /* * Example for finding an item (service) in the ListView and if found * scroll to the item. * * In this case to ensure SQLEXPRESS service is running. */ var listViewItem = ServicesListView.FindItemWithText("MSSQL$SQLEXPRESS"); if (listViewItem != null) { var index = ServicesListView.Items.IndexOf(listViewItem); ServicesListView.Items[index].Selected = true; ServicesListView.EnsureVisible(index); } else { ServicesListView.Items[0].Selected = true; ServicesListView.EnsureVisible(0); } ActiveControl = ServicesListView; } finally { ServicesListView.EndUpdate(); } }