Skip to content

Instantly share code, notes, and snippets.

@veikkoeeva
Last active March 17, 2016 08:06
Show Gist options
  • Save veikkoeeva/2e29a38d9c91637fdaa0 to your computer and use it in GitHub Desktop.
Save veikkoeeva/2e29a38d9c91637fdaa0 to your computer and use it in GitHub Desktop.
System information query example
/// <summary>
/// An interface to query system information.
/// </summary>
/// <remarks>
/// The system WMI tables are listed at <see href="https://msdn.microsoft.com/en-us/library/bg126473(v=vs.85).aspx">WMI/MI/OMI Providers</see>
/// Programmatically the tables can be queries like
/// <code>
/// var wmiSearcher = new ManagementObjectSearcher("SELECT * FROM meta_class WHERE __CLASS LIKE 'Win32_%'");
/// foreach(var obj in wmiSearcher.Get()) Console.WriteLine(obj["__CLASS"]);
/// </code>
/// In PowerShell the registry can be queries like
/// <code>
/// Get-CimClass -ClassName win32* | where CimClassMethods -ne $null | ft CimClassName, CimClassMethods -wrap -auto
/// </code>.
/// </remarks>
public interface ISystemInformation
{
/// <summary>
/// Queries the active video card information from system.
/// </summary>
/// <returns></returns>
ReadOnlyCollection<RecordSet> GetActiveVideoAdapterInformation();
}
/// <summary>
/// One set of records as returned by <see cref="SystemInformation"/> function.
/// </summary>
[Serializable]
//[DebuggerTypeProxy(typeof(CollectionDebugView<>))]
//[DebuggerDisplay("Count = {Count}")]
public class RecordSet: ICollection<KeyValuePair<string, string>>
{
/// <summary>
/// The set of records as returned by<see cref= "SystemInformation" /> function.
/// </summary>
private List<KeyValuePair<string, string>> Records { get; }
/// <summary>
/// The set of records as returned by<see cref= "SystemInformation" /> function.
/// </summary>
/// <param name="records">The raw records.</param>
public RecordSet(IEnumerable<KeyValuePair<string, string>> records)
{
if(nameof(records) == null)
{
throw new ArgumentNullException(nameof(records));
}
Records = new List<KeyValuePair<string, string>>(records);
}
public int Count { get { return Records.Count; } }
public bool IsReadOnly { get { return false; } }
public void Add(KeyValuePair<string, string> item)
{
Records.Add(item);
}
public void Clear()
{
Records.Clear();
}
public bool Contains(KeyValuePair<string, string> item)
{
return Records.Contains(item);
}
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
{
Records.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<string, string> item)
{
return Records.Remove(item);
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return Records.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Records.GetEnumerator();
}
}
}
/// <summary>
/// A default implementatio for <see cref="ISystemInformation"/>.
/// </summary>
public class SystemInformation: ISystemInformation
{
/// <summary>
/// The default query to find active video adapters from the system.
/// </summary>
/// <remarks>When CurrentBitsPerPixel and Description are not NULL, it is an active video controller.</remarks>
public const string DefaultActiveVideoAdapterInformationQuery = "SELECT Name, DriverDate, DriverVersion, AdapterRAM, CurrentBitsPerPixel, VideoModeDescription FROM Win32_VideoController WHERE CurrentBitsPerPixel IS NOT NULL AND Description IS NOT NULL";
/// <summary>
/// A query to find active video adapters from the system.
/// </summary>
public string ActiveVideoAdapterInformationQuery { get; }
/// <summary>
/// The default constructor.
/// </summary>
/// <param name="activeVideoAdapterInformationQuery">The default query to find active video adapters from the system.</param>
public SystemInformation2(string activeVideoAdapterInformationQuery = DefaultActiveVideoAdapterInformationQuery)
{
if(string.IsNullOrWhiteSpace(activeVideoAdapterInformationQuery))
{
throw new ArgumentException(activeVideoAdapterInformationQuery);
}
ActiveVideoAdapterInformationQuery = activeVideoAdapterInformationQuery;
}
/// <summary>
/// Queries the active video card information from system.
/// </summary>
/// <returns></returns>
public ReadOnlyCollection<RecordSet> GetActiveVideoAdapterInformation()
{
return GetWmiData(ActiveVideoAdapterInformationQuery);
}
private static ReadOnlyCollection<RecordSet> GetWmiData(string query)
{
using(var searcher = new ManagementObjectSearcher(query))
{
using(var items = searcher.Get())
{
return items.Cast<ManagementBaseObject>().Select(managementObjectCollection => new RecordSet(managementObjectCollection.Properties.Cast<PropertyData>().Select(y => new KeyValuePair<string, string>(y.Name, y.Value.ToString())))).ToList().AsReadOnly();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment