using BytecodeApi.Extensions;
using System.Diagnostics;
using System.Management;
namespace BytecodeApi.Wmi;
///
/// Represents a WMI namespace.
///
[DebuggerDisplay($"{nameof(WmiNamespace)}: Path = {{Path}}")]
public sealed class WmiNamespace
{
///
/// Gets the path of this .
///
public string Path { get; }
internal string FullPath => System.IO.Path.Combine(@"\\", Environment.MachineName, "ROOT", Path);
internal WmiNamespace(string path)
{
Path = path;
}
///
/// Retrieves all WMI namespaces that are within this .
///
///
/// A [] with all WMI namespaces that are within this .
///
public WmiNamespace[] GetNamespaces()
{
using ManagementClass managementClass = new(FullPath, "__namespace", new ObjectGetOptions());
using ManagementObjectCollection objectCollection = managementClass.GetInstances();
return objectCollection
.Cast()
.AsDisposable()
.Select(obj => new WmiNamespace(System.IO.Path.Combine(Path, obj["Name"].ToString() ?? throw new ManagementException("Namespace name is empty."))))
.ToArray();
}
///
/// Gets a identified by a path.
///
/// The path that identifies the .
///
/// A new object.
///
public WmiNamespace GetNamespace(string path)
{
Check.ArgumentNull(path);
return new(System.IO.Path.Combine(Path, path));
}
///
/// Retrieves all WMI classes from this .
///
///
/// A [] with all WMI classes from this .
///
public WmiClass[] GetClasses()
{
using ManagementObjectSearcher searcher = new(FullPath, "SELECT * FROM meta_class");
using ManagementObjectCollection objects = searcher.Get();
return objects
.Cast()
.AsDisposable()
.Select(obj => new WmiClass(this, obj.ClassPath.ClassName))
.ToArray();
}
///
/// Gets a identified by a name.
///
/// The name that identifies the .
///
/// A new object.
///
public WmiClass GetClass(string name)
{
Check.ArgumentNull(name);
return new(this, name);
}
}