-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWmiNamespace.cs
More file actions
84 lines (76 loc) · 2.59 KB
/
WmiNamespace.cs
File metadata and controls
84 lines (76 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using BytecodeApi.Extensions;
using System.Diagnostics;
using System.Management;
namespace BytecodeApi.Wmi;
/// <summary>
/// Represents a WMI namespace.
/// </summary>
[DebuggerDisplay($"{nameof(WmiNamespace)}: Path = {{Path}}")]
public sealed class WmiNamespace
{
/// <summary>
/// Gets the path of this <see cref="WmiNamespace" />.
/// </summary>
public string Path { get; }
internal string FullPath => System.IO.Path.Combine(@"\\", Environment.MachineName, "ROOT", Path);
internal WmiNamespace(string path)
{
Path = path;
}
/// <summary>
/// Retrieves all WMI namespaces that are within this <see cref="WmiNamespace" />.
/// </summary>
/// <returns>
/// A <see cref="WmiNamespace" />[] with all WMI namespaces that are within this <see cref="WmiNamespace" />.
/// </returns>
public WmiNamespace[] GetNamespaces()
{
using ManagementClass managementClass = new(FullPath, "__namespace", new ObjectGetOptions());
using ManagementObjectCollection objectCollection = managementClass.GetInstances();
return objectCollection
.Cast<ManagementObject>()
.AsDisposable()
.Select(obj => new WmiNamespace(System.IO.Path.Combine(Path, obj["Name"].ToString() ?? throw new ManagementException("Namespace name is empty."))))
.ToArray();
}
/// <summary>
/// Gets a <see cref="WmiNamespace" /> identified by a path.
/// </summary>
/// <param name="path">The path that identifies the <see cref="WmiNamespace" />.</param>
/// <returns>
/// A new <see cref="WmiNamespace" /> object.
/// </returns>
public WmiNamespace GetNamespace(string path)
{
Check.ArgumentNull(path);
return new(System.IO.Path.Combine(Path, path));
}
/// <summary>
/// Retrieves all WMI classes from this <see cref="WmiNamespace" />.
/// </summary>
/// <returns>
/// A <see cref="WmiClass" />[] with all WMI classes from this <see cref="WmiNamespace" />.
/// </returns>
public WmiClass[] GetClasses()
{
using ManagementObjectSearcher searcher = new(FullPath, "SELECT * FROM meta_class");
using ManagementObjectCollection objects = searcher.Get();
return objects
.Cast<ManagementObject>()
.AsDisposable()
.Select(obj => new WmiClass(this, obj.ClassPath.ClassName))
.ToArray();
}
/// <summary>
/// Gets a <see cref="WmiClass" /> identified by a name.
/// </summary>
/// <param name="name">The name that identifies the <see cref="WmiClass" />.</param>
/// <returns>
/// A new <see cref="WmiClass" /> object.
/// </returns>
public WmiClass GetClass(string name)
{
Check.ArgumentNull(name);
return new(this, name);
}
}