PDA

View Full Version : Problem using WMI statements (C# code)


MrSeanKon
05-25-2007, 09:41 AM
Here is the source code comments explain what is going on.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;

public class Form1 : System.Windows.Forms.Form
{
private Label label1;
private Label label2;
private System.ComponentModel.Container components = null;

#region Initialization and dispose
public Form1()
{
InitializeComponent();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion

#region Windows Form Designer generated code
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources=new System.ComponentModel.ComponentResourceManager(typ eof(Form1));
this.label1=new System.Windows.Forms.Label();
this.label2=new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize=true;
this.label1.Font=new System.Drawing.Font("Microsoft Sans Serif",12F,System.Drawing.FontStyle.Bold,System.Drawing. GraphicsUnit.Point,((byte) (161)));
this.label1.Location=new System.Drawing.Point(78,30);
this.label1.Name="label1";
this.label1.Size=new System.Drawing.Size(57,20);
this.label1.TabIndex=0;
this.label1.Text="label1";
//
// label2
//
this.label2.AutoSize=true;
this.label2.Font=new System.Drawing.Font("Microsoft Sans Serif",12F,((System.Drawing.FontStyle) ((System.Drawing.FontStyle.Bold|System.Drawing.Fon tStyle.Italic))),System.Drawing.GraphicsUnit.Point ,((byte) (161)));
this.label2.Location=new System.Drawing.Point(78,63);
this.label2.Name="label2";
this.label2.Size=new System.Drawing.Size(57,20);
this.label2.TabIndex=1;
this.label2.Text="label2";
//
// Form1
//
this.AutoScaleBaseSize=new System.Drawing.Size(5,13);
this.ClientSize=new System.Drawing.Size(226,122);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle=System.Windows.Forms.FormBord erStyle.FixedDialog;
this.Icon=((System.Drawing.Icon) (resources.GetObject("$this.Icon")));
this.MaximizeBox=false;
this.Name="Form1";
this.ShowIcon=false;
this.Text="System information";
this.Load+=new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion

private void Form1_Load(object sender,EventArgs e)
{
ManagementObjectSearcher query;
ManagementObjectCollection queryCollection;
System.Management.ObjectQuery oq;

oq=new System.Management.ObjectQuery("SELECT * FROM Win32_Processor");
query=new ManagementObjectSearcher(oq); // The program retrieves CPU frequency
queryCollection=query.Get(); // from registry but the CIM_Fan statement
foreach (ManagementObject mo in queryCollection) // does not show anything
{
label1.Text=mo["CurrentClockSpeed"].ToString()+ " MHz";
break;
}
oq=new System.Management.ObjectQuery("SELECT * FROM CIM_Fan");
query=new ManagementObjectSearcher(oq);
queryCollection=query.Get();
foreach (ManagementObject mo in queryCollection)
{
label2.Text=mo["Description"].ToString();
break;
}
}
}

Kreij
05-25-2007, 01:36 PM
CIM_Fan has a property called "Availability" that is inherited from CIM_LogicalDevice.
Try querying CIM_LogicalDevice directly and seeing what you get.
You could also try querying CIM_CoolingDevice.

Kreij
05-25-2007, 01:54 PM
Another thing you could try is replacing label2 with a multiline textbox.


foreach (ManagementObject mo in queryCollection)
{
textBox1.Text=mo["Description"].ToString() + "\n";
// remove the break so you see all mo descriptions
// in case there are more than 1
}


Perhaps the first mo's description is null. Since you are "breaking" out of the foreach after
the first one you may not being seeing later ones.

MrSeanKon
05-29-2007, 03:30 PM
Thanks for you effort Kreij but none of these statements work. :)
gizmo (at Aoaforums) said Win32_Fan, CIM_TemperatureSensor, and many other classes rely on the drivers to provide the necessary information. If the drivers do not provide the information via WMI, then you won't be able to read it.
Therefore it is not Microsoft's fault.