Skip to content

Commit dd222fc

Browse files
authored
Add files via upload
1 parent d689143 commit dd222fc

7 files changed

+468
-0
lines changed

WmiClassTester/Form1.Designer.cs

+154
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WmiClassTester/Form1.cs

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System.Management;
2+
3+
namespace WmiClassTester
4+
{
5+
public partial class MainForm : Form
6+
{
7+
private static string wmiScope = @"\\.\ROOT\Cimv2";
8+
private static string wmiStandardScope = @"\\.\ROOT\StandardCimv2";
9+
10+
private Dictionary<string, WmiInfo> wmiMappings;
11+
12+
public MainForm()
13+
{
14+
InitializeComponent();
15+
PopulateControls();
16+
}
17+
18+
private void PopulateControls()
19+
{
20+
wmiMappings = new Dictionary<string, WmiInfo>
21+
{
22+
{ "Global Settings", new WmiInfo("MSFT_NetSecuritySettingData", wmiStandardScope) },
23+
{ "Firewall Profiles", new WmiInfo("MSFT_NetFirewallProfile", wmiStandardScope) },
24+
{ "Firewall Rules", new WmiInfo("MSFT_NetFirewallRule", wmiStandardScope) },
25+
{ "Network Adapters", new WmiInfo("Win32_NetworkAdapter", wmiScope) }
26+
};
27+
28+
foreach (var item in wmiMappings.Keys)
29+
{
30+
comboTemplates.Items.Add(item);
31+
}
32+
33+
comboTemplates.SelectedIndex = -1; // Ensure no item is selected initially
34+
}
35+
36+
private void btnQuery_Click(object sender, EventArgs e)
37+
{
38+
string wmiClass = txtWmiClass.Text.Trim();
39+
40+
if (string.IsNullOrEmpty(wmiClass))
41+
{
42+
MessageBox.Show("Please enter a WMI class name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
43+
return;
44+
}
45+
46+
try
47+
{
48+
// Clear existing results
49+
dataGridViewResults.Rows.Clear();
50+
dataGridViewResults.Columns.Clear();
51+
52+
// Connect to WMI namespace
53+
ManagementScope scope = new ManagementScope(txtWmiScope.Text.Trim());
54+
scope.Connect();
55+
56+
// Query the WMI class
57+
string query = $"SELECT * FROM {wmiClass}";
58+
ObjectQuery objectQuery = new ObjectQuery(query);
59+
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, objectQuery);
60+
ManagementObjectCollection results = searcher.Get();
61+
62+
// Dynamically create columns based on the properties of the first instance
63+
bool columnsCreated = false;
64+
65+
foreach (ManagementObject obj in results)
66+
{
67+
if (!columnsCreated)
68+
{
69+
foreach (PropertyData property in obj.Properties)
70+
{
71+
dataGridViewResults.Columns.Add(property.Name, property.Name);
72+
}
73+
columnsCreated = true;
74+
}
75+
76+
// Add a new row for each instance
77+
DataGridViewRow row = new DataGridViewRow();
78+
row.CreateCells(dataGridViewResults);
79+
80+
int columnIndex = 0;
81+
foreach (PropertyData property in obj.Properties)
82+
{
83+
object value = property.Value;
84+
row.Cells[columnIndex].Value = value ?? "NULL";
85+
columnIndex++;
86+
}
87+
88+
dataGridViewResults.Rows.Add(row);
89+
}
90+
91+
if (!columnsCreated)
92+
{
93+
labelError.Text = "No properties found for the specified WMI class.";
94+
}
95+
else
96+
{
97+
labelError.Text = string.Empty;
98+
}
99+
}
100+
catch (Exception ex)
101+
{
102+
//MessageBox.Show($"Error querying WMI class: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
103+
labelError.Text = $"Error querying WMI class: {ex.Message}";
104+
}
105+
}
106+
107+
private void comboTemplates_SelectedIndexChanged(object sender, EventArgs e)
108+
{
109+
string selectedItem = comboTemplates.SelectedItem?.ToString();
110+
111+
if (!string.IsNullOrEmpty(selectedItem) && wmiMappings.TryGetValue(selectedItem, out WmiInfo wmiInfo))
112+
{
113+
txtWmiClass.Text = wmiInfo.WmiClass;
114+
txtWmiScope.Text = wmiInfo.WmiScope;
115+
}
116+
}
117+
118+
private struct WmiInfo
119+
{
120+
public string WmiClass { get; set; }
121+
public string WmiScope { get; set; }
122+
123+
public WmiInfo(string wmiClass, string wmiScope)
124+
{
125+
WmiClass = wmiClass;
126+
WmiScope = wmiScope;
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)