コード例 #1
0
        private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboCategory.Text.Length > 0)
            {
                try
                {
                    PerformanceCounter[] pcs;
                    cboCounter.Items.Clear();
                    cboCounter.Text = "";
                    cboInstance.Items.Clear();
                    cboInstance.Text = "";

                    PerformanceCounterCategory pcCat = new PerformanceCounterCategory(cboCategory.Text, txtComputer.Text);
                    string instanceName = "";
                    if (pcCat.CategoryType == PerformanceCounterCategoryType.MultiInstance)
                    {
                        string[] instances = pcCat.GetInstanceNames();
                        foreach (string instanceNameItem in instances)
                        {
                            cboInstance.Items.Add(instanceNameItem);
                        }
                        if (instances.Length > 0)
                        {
                            instanceName = pcCat.GetInstanceNames()[0];
                        }
                        pcs = pcCat.GetCounters(instanceName);
                    }
                    else
                    {
                        pcs = pcCat.GetCounters();
                    }
                    foreach (PerformanceCounter pc in pcs)
                    {
                        cboCounter.Items.Add(pc.CounterName);
                    }
                    if (InitialCounter != null && InitialCounter.Length > 0)
                    {
                        for (int i = 0; i < cboCounter.Items.Count; i++)
                        {
                            if (cboCounter.Items[i].ToString().ToLower() == InitialCounter.ToLower())
                            {
                                cboCounter.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (InitialInstance != null && InitialInstance.Length > 0)
                    {
                        for (int i = 0; i < cboInstance.Items.Count; i++)
                        {
                            if (cboInstance.Items[i].ToString().ToLower() == InitialInstance.ToLower())
                            {
                                cboInstance.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            CheckForValidCounter();
        }
コード例 #2
0
        private void lvwCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwCategories.SelectedItems.Count > 0)
            {
                try
                {
                    PerformanceCounter[] pcs;
                    lvwCounters.Items.Clear();
                    lvwInstances.Items.Clear();
                    string category    = lvwCategories.SelectedItems[0].Text;
                    string machineName = ConfigVariables.ApplyOn(txtComputer.Text);

                    PerformanceCounterCategory pcCat = new PerformanceCounterCategory(category, machineName);
                    string instanceName = "";
                    if (pcCat.CategoryType == PerformanceCounterCategoryType.MultiInstance)
                    {
                        string[] instances = pcCat.GetInstanceNames();
                        foreach (string instanceNameItem in (from string s in instances
                                                             orderby s
                                                             select s))
                        {
                            ListViewItem lvi = new ListViewItem(instanceNameItem);
                            if (instanceNameItem.ToLower() == InitialInstance.ToLower())
                            {
                                lvi.Selected = true;
                            }
                            lvwInstances.Items.Add(lvi);
                        }
                        if (instances.Length > 0)
                        {
                            instanceName = pcCat.GetInstanceNames()[0];
                        }
                        pcs = pcCat.GetCounters(instanceName);
                    }
                    else
                    {
                        pcs = pcCat.GetCounters();
                    }
                    foreach (PerformanceCounter pc in (from p in pcs
                                                       orderby p.CounterName
                                                       select p))
                    {
                        ListViewItem lvi = new ListViewItem(pc.CounterName);
                        if (pc.CounterName.ToLower() == InitialCounter.ToLower())
                        {
                            lvi.Selected = true;
                        }
                        lvwCounters.Items.Add(lvi);
                    }
                    if (lvwInstances.SelectedItems.Count > 0)
                    {
                        lvwInstances.SelectedItems[0].EnsureVisible();
                    }
                    if (lvwCounters.SelectedItems.Count > 0)
                    {
                        lvwCounters.SelectedItems[0].EnsureVisible();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            CheckForValidCounter();
        }