##Conventions
Conventions
- Use meaningful and camel-cased names
var GI1GIUS.. - Use
varwhen the instance type is the same as the declaring typeCimSession session =
##Design
Design
I suggest to create a class to store the info you require from the cim interface.
class CimInfo
{
public CimInfo(CimInstance cim)
{
UserName = GetProperty(cim, "UserName");
BootUpState = GetProperty(cim, "BootUpState");
Manufacturer = GetProperty(cim, "Manufacturer");
Model = GetProperty(cim, "Model");
}
private static string GetProperty(CimInstance cim, string name)
{
if (cim == null) throw new ArgumentNullException(nameof(cim));
return cim.CimInstanceProperties[name].Value.ToString();
}
public string UserName { get; }
public string BootUpState { get; }
public string Manufacturer { get; }
public string Model { get; }
}
The async operations can be merged and rewritten using the new class. Since you only have need of one cim instance, we could also avoid looping all queried results.
Notes:
- Perhaps there is an alternative available for
QueryInstancesthat only returns the first result. - If
CimSessionimplementsIDisposable, use ausingblock for it.
snippet
var cimInfo = await Task.Run(() =>
{
var session = CimSession.Create(computerHostName);
var queryResults = session.QueryInstances(nameSpace, WQL,
"SELECT Username, BootUpState, Manufacturer, Model FROM Win32_ComputerSystem");
return new CimInfo(queryResults.FirstOrDefault());
});
And the output could be
TextBoxUserName.Text = cimInfo.UserName;
TextBoxBootUpState.Text = cimInfo.BootUpState;
TextBoxManufacturer.Text = cimInfo.Manufacturer;
TextBoxModel.Text = cimInfo.Model;