class WMIWrapper
{
private string m_ComputerName = string.Empty;
private string m_UserName = string.Empty;
private string m_Password = string.Empty;
private ManagementScope Ms = null;
private bool? bLocalConnect = null;
public WMIWrapper(string computerName)
{
this.m_ComputerName = computerName;
this.bLocalConnect = true;
}
public WMIWrapper(string computerName, string userName, string password)
{
this.m_ComputerName = computerName;
this.m_UserName = userName;
this.m_Password = password;
this.bLocalConnect = false;
}
private void Connect()
{
if (true == bLocalConnect)
{
if (null == Ms)
{
Ms = new ManagementScope("\\\\" + m_ComputerName + "\\root\\cimv2");
}
}
else
{
ConnectionOptions con = new ConnectionOptions();
con.Username = m_UserName;
con.Password = m_Password;
if (null == Ms)
{
Ms = new ManagementScope("\\\\" + m_ComputerName + "\\root\\cimv2", con);
}
}
try
{
Ms.Connect();
}
catch (ManagementException ex )
{
throw ex;
}
}
public ManagementObjectCollection ExecuteQuery(string queryString)
{
this.Connect();
ObjectQuery Mq = new ObjectQuery(queryString);
ManagementObjectSearcher Mos = new ManagementObjectSearcher(Ms, Mq);
return Mos.Get();
}
public string GetValueByProperityName(string queryString,string properityName)
{
this.Connect();
ObjectQuery Mq = new ObjectQuery(queryString);
ManagementObjectSearcher Mos = new ManagementObjectSearcher(Ms, Mq);
ManagementObjectCollection col = Mos.Get();
string strValue = null;
if (col.Count != 0)
{
foreach (ManagementObject m in col)
{
strValue = m.Properties[properityName].Value.ToString();
break;
}
}
return strValue;
}
}
posted on 2007-07-19 23:06
GuangMing Lan 阅读(154)
评论(0) 编辑 收藏 引用