星期三, 2月 04, 2009

利用WQL 找Windows裡相關資訊

先引用WQL所在的namespace
using System.Management; //要先在專案的參考加入System.Management


找Driver
public bool getDrivers()
{
ManagementScope scope = new ManagementScope(@"root\CIMV2");
ObjectQuery query = new ObjectQuery("WQL", "SELECT * FROM Win32_SystemDriver");
ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection col = search.Get();

if (col.Count == 0)
{
return false;
}

Console.WriteLine("Win32_SystemDriver instance (Total Found:{0})", col.Count.ToString());
Console.WriteLine("-----------------------------------");
foreach (ManagementObject obj in col)
{
string driverlist = obj.ToString().ToUpper();
if (driverlist.Contains("SER2PL")) //此例是找SER2PL,更換你要的Driver Name
{
Console.WriteLine("找到Driver已安裝,Name: {0}", obj.ToString()); //name 是 Unique identifier for the service
return true;
}
/*
//印出所有屬性
foreach (PropertyData propertyData in obj.Properties)
{
ManagementBaseObject mbo = null;
if ((mbo = propertyData.Value as ManagementBaseObject) != null)
{
foreach (PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
}
}
* */
}
search.Dispose();
col.Dispose();

return false;
}


References:
可直接下拉查詢相關的資訊
WMI Code Creator v1.0

沒有留言: