Pregunta

i am reading processor's details WIN32_PROCESSOR using WMI. Currently i am trying to read these property:SELECT CAPTION,L2CACHESIZE,L3CACHESIZE FROM WIN32_PROCESSOR. So that i am working with below code:

static void Main(string[] args)
{
    string strQuery = "SELECT CAPTION,L2CACHESIZE,L3CACHESIZE FROM WIN32_PROCESSOR";
    string strIPAddress = "XXX.XXX.X.XXX";
    DataTable dtProcessor = new DataTable();
    dtProcessor.Columns.Add("CAPTION");
    dtProcessor.Columns.Add("L2CACHESIZE");
    dtProcessor.Columns.Add("L3CACHESIZE");            

    ManagementScope scope = new ManagementScope(@"\\" + strIPAddress + @"\root\cimv2");
    SelectQuery query = new SelectQuery();
    query.QueryString = strQuery;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection queryCollection = searcher.Get();
    foreach (ManagementObject mngmntObj in queryCollection)
    {
        DataRow dr = dtProcessor.NewRow();
        dr["CAPTION"] = mngmntObj["CAPTION"];
        dr["L2CACHESIZE"] = mngmntObj["L2CACHESIZE"];
        dr["L3CACHESIZE"] = mngmntObj["L3CACHESIZE"];
        dtProcessor.Rows.Add(dr);

    }
}

This is working fine on my Windows Server 2008R2 machine, but the same code giving me exception of System.Management.ManagementException -> Invalid Query because L3CACHESIZE is not present in XP as discussed here. What can be best way to handle this with reading values which are present ?

¿Fue útil?

Solución

If you want check if a wmi property exist , you can use the ManagementBaseObject.Properties Property and iterate over the collection.

Try something like so

static void Main(string[] args)
{
    string strQuery = "SELECT * FROM WIN32_PROCESSOR";
    string strIPAddress = "XXX.XXX.X.XXX";
    DataTable dtProcessor = new DataTable();
    dtProcessor.Columns.Add("CAPTION");
    dtProcessor.Columns.Add("L2CACHESIZE");
    dtProcessor.Columns.Add("L3CACHESIZE");            

    ManagementScope scope = new ManagementScope(@"\\" + strIPAddress + @"\root\cimv2");
    SelectQuery query = new SelectQuery();
    query.QueryString = strQuery;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection queryCollection = searcher.Get();
    List<string> properties = new List<string>();
    foreach (ManagementObject mngmntObj in queryCollection)
    {

       if (properties.Count==0)
       {
         foreach (PropertyData property in mngmntObj.Properties)
         properties.Add(property.Name);
       }

        DataRow dr = dtProcessor.NewRow();
        dr["CAPTION"] = mngmntObj["CAPTION"];
        dr["L2CACHESIZE"] = mngmntObj["L2CACHESIZE"];
        if (properties.Contains("L3CACHESIZE", StringComparer.OrdinalIgnoreCase))
        {
        dr["L3CACHESIZE"] = mngmntObj["L3CACHESIZE"];
        }
        dtProcessor.Rows.Add(dr);

    }
}

Also for this particular case if you want get the info related to the memory cache try the Win32_CacheMemory class

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top