標籤: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實現對驅動的查詢