-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
63 lines (56 loc) · 1.75 KB
/
Program.cs
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
using System;
using System.Management;
namespace App {
class Program {
// data
/// <summary>
/// WMI Namespace.
/// </summary>
private static string WmiNs = "root\\WMI";
// static methods
/// <summary>
/// You have to dream before your dreams can come true.
/// : A. P. J. Abdul Kalam
/// </summary>
/// <param name="args">Input arguments.</param>
static void Main(string[] args) {
if (args.Length == 0) Get(args);
else Set(args);
}
/// <summary>
/// Get monitor brightness.
/// </summary>
/// <param name="args">NA.</param>
private static void Get(string[] args) {
ManagementObject o = Obj(WmiNs, "WmiMonitorBrightness");
Console.WriteLine(o == null ? "-1" : o.GetPropertyValue("CurrentBrightness"));
}
/// <summary>
/// Set monitor brightness.
/// </summary>
/// <param name="args">brightness.</param>
private static void Set(string[] args) {
double v = 0;
double.TryParse(args[0], out v);
ManagementObject o = Obj(WmiNs, "WmiMonitorBrightnessMethods");
if (o == null) Console.Error.WriteLine("err: WMI interfacing failed");
else o.InvokeMethod("WmiSetBrightness", new object[] { uint.MaxValue, (byte)v });
}
/// <summary>
/// Get object of specified Management class.
/// </summary>
/// <param name="ns">Namespace.</param>
/// <param name="cls">Class.</param>
/// <returns>Management object.</returns>
private static ManagementObject Obj(string ns, string cls) {
ManagementScope scp = new ManagementScope(ns);
SelectQuery qry = new SelectQuery(cls);
using(ManagementObjectSearcher srch = new ManagementObjectSearcher(scp, qry)) {
using(ManagementObjectCollection coll = srch.Get()) {
foreach (ManagementObject obj in coll) return obj;
}
}
return null;
}
}
}