DEV Community

nicoriff
nicoriff

Posted on

New .NET library for those who work with WMI. Simpler than ever.

ORMi is a new .NET library I wrote to communicate to WMI (Windows Management Instrumentation). I´ve made several dozens of WMI compatible apps over these years and I always came to the conclusion that the .NET standard libraries are very poor and makes developer write too much code to get simple values. That´s where ORMi stands. To make every WMI work much more easy and to make developers write more readable, easy to understand and mantainable code.
I´ve added several improvements since the first versions of ORMi such as async methods, WMI watcher and WMI method support.

Anyone that has worked with WMI and .NET can tell you that WMI work can be a headache. WMI class names are not the simpler ones and definitvely not the more intuituve. The have tricky and non conventional names. I bet you would never want give that name to a class on your project. That´s one thing that ORMi makes simpler and more intuitive.

Let me show you how it works:

To get the full experience with ORMi it is important to have our model defined and mapped to a WMI Class. Let's suppose that we want to get a list of devices connected to the PC. That is quite simple:

[WMIClass("Win32_PnPEntity")]  
public class Device  
{  
    public string Name { get; set; }  

    public string Description { get; set; }  

    [WMIProperty("Status")]  
    public string StatusName { get; set; }  
}

The above class is enhanced with the use of some attributes. WMIClass tells ORMi what is the name of the WMIClass that Device represents. WMIProperty attribute tells which is the WMI property that will be mapped to that property. ORMi works like this: If there is no attribute, then the member name is used to do the mapping. If an attribute has been set, then the attribute name is used.

Then, with our model properly set we can query WMI to get the instances we want:

static void Main(string[] args)  
{  
    WMIHelper helper = new WMIHelper("root\\CimV2");
    List<Device> devices = helper.Query<Device>().ToList();  
}

This way for example if you only want to search for a mouse device, you can query this way:

List<Device> devices = helper.Query<Device>().Where(p => p.Description.Contains("Mouse")).ToList();

ORMi also supports all of CRUD operations. You can add, modify or delete instances in a quite simple way.

 Person person = new Person
     {
         FirstName = "John",
         Lastname = "Doe",
         DocumentNumber = "9995",
     };
 helper.AddInstance(person);

The class defined in this example is defined this way:

[WMIClass("Lnl_CardHolder")]
public class Person
{
    public string Lastname { get; set; }
    public string FirstName { get; set; }

    [WMIProperty( Name = "SSNO", SearchKey = true)]
    public string DocumentNumber { get; set; }

    [WMIIgnore]
    public int Age { get; set; }
}

All these operations are also supported in async fashion.

You are invited to suggest improvements, fixes and contribute to the project!.

Project Repository and docs: https://github.com/nicoriff/ORMi

You can download via NuGet: https://www.nuget.org/packages/ORMi/

Top comments (0)