C#中用WMI實現對驅動的查詢

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   color   os   sp   for   

 

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);            }        }
  }
}

僅僅實現了查詢的功能,後續有待加入與裝置管理員類似的管理功能!

C#中用WMI實現對驅動的查詢

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.