public Namespace GetNamespace(INamespaceParent parent, string relativeName, bool ignoreCase) { // WMI is very slow, so do everything possible to minimise WMI access. We need to load all the // namespaces in the hierarchy (from the parent to the one being sought), because any of them // may have enabled events. Get them all in one query. // Build a query that gets all the namespaces we need (if they exist). string[] nsParts = relativeName.Split('.'); string parentFullName = parent.FullName; string queryString = BuildQueryStringForNamespaces(nsParts, parentFullName); // WQL is case-insensitive, so if ignoreCase is false use the Hashtable created below to filter. Hashtable namespaces = ignoreCase ? new Hashtable(StringComparer.InvariantCultureIgnoreCase) : new Hashtable(); var searcher = new ManagementObjectSearcher(_scope, new ObjectQuery(queryString)); // Read the namespaces returned and store them temporarily in a Hasthable. We cannot create // Namespace objects as we go, because they may be returned out of order (ie. children before parents). using (ManagementObjectCollection configs = searcher.Get()) { foreach (ManagementObject config in configs) { var details = new NamespaceDetails(); var nsName = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty); var nsParentName = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.ParentProperty); var enabledEvents = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.Namespace.EnabledEventsProperty); var mixedEvents = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.Namespace.MixedEventsProperty); details._enabledEvents = SplitString(enabledEvents, Constants.Module.ListSeparator); details._mixedEvents = SplitString(mixedEvents, Constants.Module.ListSeparator); namespaces.Add(CatalogueName.GetFullNameUnchecked(nsParentName, nsName), details); } } // Now that we have all the details create the hierarchy of Namespace objects. Catalogue catalogue = parent.Catalogue; Namespace ns = null; foreach (string nsName in nsParts) { string nsFullName = CatalogueName.GetFullNameUnchecked(parentFullName, nsName); var details = (NamespaceDetails)namespaces[nsFullName]; if (details == null) { return(null); // One of the namespaces in the path was not found in WMI. } ns = catalogue.CreateNamespace(parent, nsName, details._enabledEvents, details._mixedEvents); parent.AddIfNotLoaded(ns); parentFullName = nsFullName; parent = ns; } return(ns); }