-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWmiClass.cs
More file actions
108 lines (102 loc) · 3.6 KB
/
WmiClass.cs
File metadata and controls
108 lines (102 loc) · 3.6 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Diagnostics;
using System.Management;
namespace BytecodeApi.Wmi;
/// <summary>
/// Represents a WMI class.
/// </summary>
[DebuggerDisplay($"{nameof(WmiClass)}: Name = {{Name}}, Namespace = {{Namespace.FullPath}}")]
public sealed class WmiClass : IWmiQueryable
{
/// <summary>
/// Gets the <see cref="WmiNamespace" /> associated with this <see cref="WmiClass" />.
/// </summary>
public WmiNamespace Namespace { get; }
/// <summary>
/// Gets the name of this <see cref="WmiClass" />.
/// </summary>
public string Name { get; }
internal WmiClass(WmiNamespace @namespace, string name)
{
Namespace = @namespace;
Name = name;
}
/// <summary>
/// Specifies what columns to read from the WMI class. By default, all columns are read.
/// </summary>
/// <param name="columns">A <see cref="string" />[] specifying what columns to read from the WMI class.</param>
/// <returns>
/// The query with the additional SELECT statement.
/// </returns>
public IWmiQueryable Select(params string[] columns)
{
return new WmiQueryBuilder(this).Select(columns);
}
/// <summary>
/// Specifies a filter condition for the WMI WHERE clause.
/// </summary>
/// <param name="condition">A statement for the WMI WHERE clause.</param>
/// <returns>
/// The query with the additional WHERE statement.
/// </returns>
public IWmiQueryable Where(string condition)
{
return new WmiQueryBuilder(this).Where(condition);
}
/// <summary>
/// Executes this query and reads the contents from the WMI class.
/// </summary>
/// <returns>
/// A new <see cref="WmiObject" />[] with the contents from the WMI class.
/// </returns>
public WmiObject[] ToArray()
{
return new WmiQueryBuilder(this).ToArray();
}
/// <summary>
/// Executes this query and reads the first element from the WMI class.
/// </summary>
/// <returns>
/// A new <see cref="WmiObject" /> with the first element from the WMI class.
/// </returns>
public WmiObject First()
{
return new WmiQueryBuilder(this).First();
}
/// <summary>
/// Executes this query and reads the first element from the WMI class, and returns <see langword="null" />, if the query returned no elements.
/// </summary>
/// <returns>
/// A new <see cref="WmiObject" /> with the first element from the WMI class, or <see langword="null" />, if the query returned no elements.
/// </returns>
public WmiObject? FirstOrDefault()
{
return new WmiQueryBuilder(this).FirstOrDefault();
}
/// <summary>
/// Invokes a method on this <see cref="WmiClass" />.
/// </summary>
/// <param name="methodName">The name of the method to invoke.</param>
/// <param name="args">An array containing parameter values.</param>
/// <returns>
/// The <see cref="object" /> value returned by the method.
/// </returns>
public object InvokeMethod(string methodName, params object[]? args)
{
Check.ArgumentNull(methodName);
using ManagementClass managementClass = new(Namespace.FullPath, Name, new ObjectGetOptions());
return managementClass.InvokeMethod(methodName, args ?? []);
}
/// <summary>
/// Invokes a method on this <see cref="WmiClass" />.
/// </summary>
/// <param name="methodName">The name of the method to invoke.</param>
/// <param name="args">An array containing parameter values.</param>
/// <typeparam name="T">The type of the returned value.</typeparam>
/// <returns>
/// The value returned by the method, casted to <typeparamref name="T" />.
/// </returns>
public T InvokeMethod<T>(string methodName, params object[]? args)
{
return (T)InvokeMethod(methodName, args);
}
}