C#中用WMI实现对驱动的查询

简介: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WMI_驱动
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            //SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");//硬盘盘符
            SelectQuery selectQuery = new SelectQuery("select * from Win32_DiskDrive");//硬盘序列号
           
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {
                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), Driver["Description"].ToString() }, -1);
                listView1.Items.Add(lvi);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();

            SelectQuery selectQuery = new SelectQuery("select * from Win32_SystemDriver");//驱动程序
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), Driver["Description"].ToString() }, -1);
                listView1.Items.Add(lvi);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

            listView1.Items.Clear();

            SelectQuery selectQuery = new SelectQuery("select * from Win32_USBController");//USB控制器
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), Driver["Description"].ToString() }, -1);
                //ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(),"" },-1);
                listView1.Items.Add(lvi);
            }


            SelectQuery selectQuery2 = new SelectQuery("select * from Win32_USBHub");//USB集线器
            ManagementObjectSearcher search2 = new ManagementObjectSearcher(selectQuery2);
            foreach (ManagementObject Driver in search2.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), Driver["Description"].ToString() }, -1);
                //ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(),"" },-1);
                listView1.Items.Add(lvi);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.Columns.Clear();//清空列记录
            ColumnHeader cZh = new ColumnHeader();//创建一个列
            cZh.Text = "名称";//列名
            cZh.Width = 300;
            ColumnHeader cCh = new ColumnHeader();
            cCh.Text = "描述";
            cCh.Width = 600;
            listView1.Columns.AddRange(new ColumnHeader[] { cZh, cCh });//将这两列加入listView1
            listView1.View = View.Details;//列的显示模式
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();


            SelectQuery selectQuery = new SelectQuery("select * from Win32_Printer ");//打印机
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(),"" }, -1);
                listView1.Items.Add(lvi);
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();


            SelectQuery selectQuery = new SelectQuery("select * from Win32_PrinterConfiguration ");//打印机设置
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), "" }, -1);
                listView1.Items.Add(lvi);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();


            SelectQuery selectQuery = new SelectQuery("select * from Win32_PrintJob ");//打印机任务
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), "" }, -1);
                listView1.Items.Add(lvi);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();


            SelectQuery selectQuery = new SelectQuery("select * from Win32_TCPIPPrinterPort ");//打印机端口
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), "" }, -1);
                listView1.Items.Add(lvi);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();


            SelectQuery selectQuery = new SelectQuery("select * from Win32_PointingDevice ");//点输入设备,鼠标
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), Driver["Description"].ToString() }, -1);
                listView1.Items.Add(lvi);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();


            SelectQuery selectQuery = new SelectQuery("select * from Win32_Keyboard");//键盘
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), Driver["Description"].ToString() }, -1);
                listView1.Items.Add(lvi);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();


            SelectQuery selectQuery = new SelectQuery("select * from Win32_Printer ");//打印机
            ManagementObjectSearcher search = new ManagementObjectSearcher(selectQuery);
            foreach (ManagementObject Driver in search.Get())
            {

                ListViewItem lvi = new ListViewItem(new string[] { Driver["Name"].ToString(), "" }, -1);
                listView1.Items.Add(lvi);
            }
        }
  }
}

仅仅实现了查询的功能,后续有待加入与设备管理器类似的管理功能!

遗失的拂晓
目录
相关文章
|
6月前
|
SQL 数据库 C#
C# .NET面试系列十一:数据库SQL查询(附建表语句)
#### 第1题 用一条 SQL 语句 查询出每门课都大于80 分的学生姓名 建表语句: ```sql create table tableA ( name varchar(10), kecheng varchar(10), fenshu int(11) ) DEFAULT CHARSET = 'utf8'; ``` 插入数据 ```sql insert into tableA values ('张三', '语文', 81); insert into tableA values ('张三', '数学', 75); insert into tableA values ('李四',
158 2
C# .NET面试系列十一:数据库SQL查询(附建表语句)
|
1月前
|
SQL 缓存 分布式计算
C#如何处理上亿级数据的查询效率
C#如何处理上亿级数据的查询效率
22 1
|
2月前
|
开发框架 自然语言处理 .NET
C#一分钟浅谈:LINQ 查询表达式的使用技巧
【9月更文挑战第6天】LINQ(Language Integrated Query)是C#开发中的强大工具,使查询数据集合变得简单且接近自然语言。本文从基础入手,通过具体示例讲解LINQ查询表达式的使用技巧,包括过滤、排序和分组等操作。同时,文章还探讨了常见问题及解决方法,如性能优化、过早枚举和类型转换等,帮助开发者写出更高效、易维护的代码。
94 15
|
6月前
|
SQL 开发框架 .NET
C#进阶-LINQ表达式之GroupBy分组查询
本篇文章我们将演示LINQ扩展包基础语法里的GroupBy分组查询,并实现投影等实际操作中常用的类型转换手法。目前LINQ支持两种语法,我会在每个案例前先用大家熟知的SQL语句表达,再在后面用C#的两种LINQ语法分别实现。LINQ语法第一次接触难免感到陌生,最好的学习方式就是在项目中多去使用,相信会有很多感悟。
276 0
|
6月前
|
C# 索引
C# | 【完全开源】手机号码归属地查询,一秒内百万次查询
这个开源项目是一个.NET库,可以通过手机号码获取号码归属地信息,包括运营商、国家、省份、城市、邮政编码、区号等信息。 该库加载了一个包含46万条数据的“中国手机号归属地信息”数据集,并实现了高速查询。在我的7年老笔记本上执行一百万次查询耗时不足一秒。
309 0
|
C# Android开发 C++
c#(nanoframework)安装单片机环境;如何使用c#写类似于c的单片机驱动
c#(nanoframework)安装单片机环境;如何使用c#写类似于c的单片机驱动
218 0
c#(nanoframework)安装单片机环境;如何使用c#写类似于c的单片机驱动
|
开发框架 .NET 编译器
C# Lambda表达式和linq表达式 之 匿名对象查询接收
C# Lambda表达式和linq表达式 之 匿名对象查询接收
|
SQL C# uml
C# SQL添加数据,删除数据,修改数据,查询数据
C# SQL添加数据,删除数据,修改数据,查询数据
217 0
C# SQL添加数据,删除数据,修改数据,查询数据
|
SQL 关系型数据库 API
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化(三)
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化
基于C#的ArcEngine二次开发37:循环查询过程的内存管理与性能优化(三)
|
SQL NoSQL C#
基于C#的ArcEngine二次开发32:属性sql查询语句与IMap,ILayer,IFeatureLayer,IFeatureClass关系
基于C#的ArcEngine二次开发32:属性sql查询语句与IMap,ILayer,IFeatureLayer,IFeatureClass关系
基于C#的ArcEngine二次开发32:属性sql查询语句与IMap,ILayer,IFeatureLayer,IFeatureClass关系