-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWmiQueryBuilder.cs
More file actions
93 lines (87 loc) · 2.83 KB
/
WmiQueryBuilder.cs
File metadata and controls
93 lines (87 loc) · 2.83 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
using BytecodeApi.Extensions;
namespace BytecodeApi.Wmi;
/// <summary>
/// Class to accumulate information about a WMI query before executing it.
/// </summary>
public sealed class WmiQueryBuilder : IWmiQueryable
{
internal readonly WmiClass Class;
internal string[]? Columns;
internal readonly List<string> Conditions;
internal WmiQueryBuilder(WmiClass @class)
{
Class = @class;
Conditions = [];
}
internal WmiQueryBuilder(WmiQueryBuilder query) : this(@query.Class)
{
Columns = query.Columns;
Conditions = query.Conditions.ToList();
}
/// <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)
{
Check.ArgumentNull(columns);
Check.ArgumentEx.ArrayValuesNotNull(columns);
Check.ArgumentEx.ArrayValuesNotStringEmpty(columns);
Check.Argument(Columns == null, nameof(columns), "This query already has a SELECT statement.");
return new WmiQueryBuilder(this)
{
Columns = 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)
{
Check.ArgumentNull(condition);
Check.ArgumentEx.StringNotEmpty(condition);
WmiQueryBuilder query = new(this);
query.Conditions.Add(condition);
return query;
}
/// <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()
{
using WmiIterator iterator = new(this);
return iterator.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()
{
using WmiIterator iterator = new(this);
return iterator.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()
{
using WmiIterator iterator = new(this);
return iterator.FirstOrDefault();
}
}