Exemple #1
0
        /// <include file='doc\PerformanceCounterInstaller.uex' path='docs/doc[@for="PerformanceCounterInstaller.CopyFromComponent"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public override void CopyFromComponent(IComponent component)
        {
            if (!(component is PerformanceCounter))
            {
                throw new ArgumentException(Res.GetString(Res.NotAPerformanceCounter));
            }

            PerformanceCounter counter = (PerformanceCounter)component;

            if (counter.CategoryName == null || counter.CategoryName.Length == 0)
            {
                throw new ArgumentException(Res.GetString(Res.IncompletePerformanceCounter));
            }

            if (Counters.Count > 0 && !CategoryName.Equals(counter.CategoryName))
            {
                throw new ArgumentException(Res.GetString(Res.NewCategory));
            }

            PerformanceCounterType counterType = PerformanceCounterType.NumberOfItems32;
            string counterHelp = string.Empty;

            if (CategoryName == null || string.Empty.Equals(CategoryName))
            {
                CategoryName = counter.CategoryName;
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    string machineName = counter.MachineName;

                    if (PerformanceCounterCategory.Exists(CategoryName, machineName))
                    {
                        string      keyPath = ServicePath + "\\" + CategoryName + "\\Performance";
                        RegistryKey key     = null;
                        try {
                            if (machineName == "." || String.Compare(machineName, SystemInformation.ComputerName, true, CultureInfo.InvariantCulture) == 0)
                            {
                                key = Registry.LocalMachine.OpenSubKey(keyPath);
                            }
                            else
                            {
                                RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "\\\\" + machineName);
                                key = baseKey.OpenSubKey(keyPath);
                            }

                            if (key == null)
                            {
                                throw new ArgumentException(Res.GetString(Res.NotCustomPerformanceCategory));
                            }

                            object systemDllName = key.GetValue("Library");
                            if (systemDllName == null || !(systemDllName is string) || String.Compare((string)systemDllName, PerfShimName, true, CultureInfo.InvariantCulture) != 0)
                            {
                                throw new ArgumentException(Res.GetString(Res.NotCustomPerformanceCategory));
                            }

                            PerformanceCounterCategory pcat = new PerformanceCounterCategory(CategoryName, machineName);
                            CategoryHelp = pcat.CategoryHelp;
                            if (pcat.CounterExists(counter.CounterName))
                            {
                                counterType = counter.CounterType;
                                counterHelp = counter.CounterHelp;
                            }
                        }
                        finally {
                            if (key != null)
                            {
                                key.Close();
                            }
                        }
                    }
                }
            }

            CounterCreationData data = new CounterCreationData(counter.CounterName, counterHelp, counterType);

            Counters.Add(data);
        }
        public override void CopyFromComponent(IComponent component)
        {
            if (!(component is PerformanceCounter))
            {
                throw new ArgumentException(Res.GetString("NotAPerformanceCounter"));
            }
            PerformanceCounter counter = (PerformanceCounter)component;

            if ((counter.CategoryName == null) || (counter.CategoryName.Length == 0))
            {
                throw new ArgumentException(Res.GetString("IncompletePerformanceCounter"));
            }
            if (!this.CategoryName.Equals(counter.CategoryName) && !string.IsNullOrEmpty(this.CategoryName))
            {
                throw new ArgumentException(Res.GetString("NewCategory"));
            }
            PerformanceCounterType counterType = PerformanceCounterType.NumberOfItems32;
            string counterHelp = string.Empty;

            if (string.IsNullOrEmpty(this.CategoryName))
            {
                this.CategoryName = counter.CategoryName;
            }
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                string machineName = counter.MachineName;
                if (PerformanceCounterCategory.Exists(this.CategoryName, machineName))
                {
                    string      name = @"SYSTEM\CurrentControlSet\Services\" + this.CategoryName + @"\Performance";
                    RegistryKey key  = null;
                    try
                    {
                        if ((machineName == ".") || (string.Compare(machineName, SystemInformation.ComputerName, StringComparison.OrdinalIgnoreCase) == 0))
                        {
                            key = Registry.LocalMachine.OpenSubKey(name);
                        }
                        else
                        {
                            key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, @"\\" + machineName).OpenSubKey(name);
                        }
                        if (key == null)
                        {
                            throw new ArgumentException(Res.GetString("NotCustomPerformanceCategory"));
                        }
                        object obj2 = key.GetValue("Library");
                        if (((obj2 == null) || !(obj2 is string)) || (string.Compare((string)obj2, "netfxperf.dll", StringComparison.OrdinalIgnoreCase) != 0))
                        {
                            throw new ArgumentException(Res.GetString("NotCustomPerformanceCategory"));
                        }
                        PerformanceCounterCategory category = new PerformanceCounterCategory(this.CategoryName, machineName);
                        this.CategoryHelp = category.CategoryHelp;
                        if (category.CounterExists(counter.CounterName))
                        {
                            counterType = counter.CounterType;
                            counterHelp = counter.CounterHelp;
                        }
                        this.CategoryType = category.CategoryType;
                    }
                    finally
                    {
                        if (key != null)
                        {
                            key.Close();
                        }
                    }
                }
            }
            CounterCreationData data = new CounterCreationData(counter.CounterName, counterHelp, counterType);

            this.Counters.Add(data);
        }