Skip to content

Commit

Permalink
Add new Query method for return dynamic object. Improved documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas authored and Nicolas committed Jun 17, 2018
1 parent 816314b commit 69ff6f8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
19 changes: 19 additions & 0 deletions ORMi.Sample/Models/Device.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ORMi.Sample.Models
{
[WMIClass("Win32_PnPEntity")]
public class Device
{
public string Name { get; set; }

public string Description { get; set; }

[WMIProperty("Status")]
public string StatusName { get; set; }
}
}
1 change: 1 addition & 0 deletions ORMi.Sample/ORMi.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Models\Device.cs" />
<Compile Include="Models\Person.cs" />
<Compile Include="Models\Process.cs" />
<Compile Include="Models\Processor.cs" />
Expand Down
19 changes: 16 additions & 3 deletions ORMi.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@ static void Main(string[] args)
{
WMIHelper helper = new WMIHelper("root\\CimV2");

//var processors = helper.QueryAsync<Processor>();
var dynDevices = helper.Query("SELECT * FROM Win32_PnPEntity");

var processors = helper.Query<Processor>();

List<Processor> procesors = helper.Query<Processor>().ToList();

List<Device> devices = helper.Query<Device>().ToList()
.Where(p => (p.Name ?? "")
.Contains("Intel")).ToList();

//foreach (Device d in devices)
//{
// Console.WriteLine(d.Name);
//}

//Person person = new Person
//{
Expand All @@ -33,8 +46,8 @@ static void Main(string[] args)

//List<Person> queryPerson = helper.Query<Person>("SELECT * FROM Lnl_Cardholder WHERE LASTNAME = 'Lopez'").ToList();

//WMIWatcher watcher = new WMIWatcher("root\\CimV2", "SELECT * FROM Win32_ProcessStartTrace", typeof(Process));
WMIWatcher watcher = new WMIWatcher("root\\CimV2", "SELECT * FROM Win32_ProcessStartTrace");
WMIWatcher watcher = new WMIWatcher("root\\CimV2", "SELECT * FROM Win32_ProcessStartTrace", typeof(Process));
//WMIWatcher watcher = new WMIWatcher("root\\CimV2", "SELECT * FROM Win32_ProcessStartTrace");
watcher.WMIEventArrived += Watcher_WMIEventArrived;

Console.ReadLine();
Expand Down
34 changes: 30 additions & 4 deletions ORMi/WMIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ public void RemoveInstance(string query)
}
}

/// <summary>
/// Runs a query against WMI. It will return a IEnumerable of dynamic type. No type mapping is done.
/// </summary>
/// <param name="query">Query to be run against WMI</param>
/// <returns></returns>
public IEnumerable<dynamic> Query(string query)
{
List<dynamic> res = new List<dynamic>();

ManagementObjectSearcher searcher;
searcher = new ManagementObjectSearcher(Scope, query);

EnumerationOptions options = new EnumerationOptions();
options.ReturnImmediately = true;

ManagementObjectCollection wmiRes = searcher.Get();

foreach (ManagementObject mo in wmiRes)
{
dynamic a = TypeHelper.LoadDynamicObject(mo);
res.Add(a);
}

return res;
}

/// <summary>
/// Runs a query against WMI. It will return all instances of the class corresponding to the WMI class set on the Type on IEnumerable.
/// </summary>
Expand Down Expand Up @@ -244,8 +270,8 @@ public async Task<IEnumerable<T>> QueryAsync<T>()
/// <summary>
/// Runs a custom query against WMI.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query">The Type of IEnumerable that will be returned</param>
/// <typeparam name="T">The Type of IEnumerable that will be returned</typeparam>
/// <param name="query">Query to be run against WMI</param>
/// <returns></returns>
public IEnumerable<T> Query<T>(string query)
{
Expand All @@ -271,8 +297,8 @@ public IEnumerable<T> Query<T>(string query)
/// <summary>
/// Runs an async query against WMI.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query">The Type of IEnumerable that will be returned</param>
/// <typeparam name="T">The Type of IEnumerable that will be returned</typeparam>
/// <param name="query">Query to be run against WMI</param>
/// <returns></returns>
public async Task<IEnumerable<T>> QueryAsync<T>(string query)
{
Expand Down

0 comments on commit 69ff6f8

Please sign in to comment.