分类: 嵌入式
2013-01-06 11:29:23
Explorer Control
这里首先介绍一下WMI的 Win32_LogicalDisk类(参考Platform SDK: Windows Management
Instrumentation),通过它我们可以查看到本地驱动器
的一些详细情况,我们还需要用到System.Management中的两个类
ManagementObjectSearcher
和ManagementOjbectCollection它们的作用主要是ManagementObjectSearcher将查询到了
ManagementOjbectCollection该对象的集合中去(这里可以获取的驱动器信息包括 驱动器
的名称
,类型,描述信息等)当然你也可以只查看驱动器的部分信息可以在
ManagementObjectSearcher类
的构造函数中这样写ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * From Win32_LogicalDisk "); (参考Platform SDK:
Windows Management Instrumentation)
代码如下:
//get drive collection
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From
Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();
//loop throught each object to get drive information
foreach ( ManagementObject mo in queryCollection)
{
switch (int.Parse( mo["DriveType"].ToString()))
{
case Removable: //removable drives
imageIndex = 5;
selectIndex = 5;
break;
case LocalDisk: //Local drives
imageIndex = 6;
selectIndex = 6;
break;
case CD: //CD rom drives
imageIndex = 7;
selectIndex = 7;
break;
case Network: //Network drives
imageIndex = 8;
selectIndex = 8;
break;
default: //defalut to folder
imageIndex = 2;
selectIndex = 3;
break;
}
//get drive name
Console.WriteLine("Drive: " + mo["Name"].ToString());
}
SystemInfo Control
该控件主要用来查看本地或远程主机的OS和硬件信息,这里需要用到另外两个
对象ConnectionOptions和ManagementScope,ConnectionOptions主要是设置
WMI连接信息的,如用户名和密码,这里我们主要用到它的两个属性UserName和
Password; ManagementScope对象表示WMI的规范,通过该规范可以访问服务器
和命名空间的路径已及使用ConnectionOptions中的设置
请看如下代码:
//Connect to the remote computer
ConnectionOptions co = new ConnectionOptions();
co.Username = textUserID.Text;
co.Password = textPassword.Text;
//Point to machine
System.Management.ManagementScope ms = new
System.Management.ManagementScope("\\\\" +
stringHostName + "\\root\\cimv2", co);
现在我们结合刚才的知识来得到我们要的信息,但我们要得到那些我们想要信息呢?
那样的话我们就必须要用到ObjectQuery对象,通过它我们可以得到我们想要的查询
信息.把ObjectQuery对象和ManagementScope对象放入ManagementObjectSearcher对象
中这样就可以通过我们设置好的规范和我们设置好的查询得到我们想要的结果,当然
还必须要掉用ManagementObjiectSearcher的Get()方法,它会返回一个ManagementObject
对象的集合,然后可以通过结合操作来访问到每一个我们要的信息.
代码如下:
//Query system for Operating System information
oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
query = new ManagementObjectSearcher(ms,oq);
queryCollection = query.Get();
foreach ( ManagementObject mo in queryCollection)
{
//create child node for operating system
createChildNode(nodeCollection, "Operating System: " + mo["Caption"]);
createChildNode(nodeCollection, "Version: " + mo["Version"]);
createChildNode(nodeCollection, "Manufacturer : " + mo["Manufacturer"]);
createChildNode(nodeCollection, "Computer Name : " +mo["csname"]);
createChildNode(nodeCollection, "Windows Directory : " + mo["WindowsDirectory"]);
}
要是你只是希望查看到本地主机的信息,你就没必要去创建 ConnectionOption,
ManagementScope,ObjectQuery 对象,你仅仅只需要把ManagementObjectSearcher
对象的结果在ManagementObjectCollection集合里去调用Get()方法既可.
代码如下:
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From
Win32_OperatingSystem");
ManagementObjectCollection queryCollection = query.Get();
这里只是介绍了OS信息的方法,其它的如Bios,Memory.Network Connection等信息的查看
只需要把查询字改改就可以了!(可参考Platform SDK: Windows Management
Instrumentation)
Service Control控件介绍:
该控件要用到一个新的查询字"SELECT * FROM Win
点击查看更多: