C#遍曆系統所安裝的印表機,使用WMI方式擷取印表機的所有屬性

來源:互聯網
上載者:User

C#裡面,雖然在 System.Drawing.Printing 這個namespace下,提供了一些對系統印表機的訪問功能,但是,說實話是太弱了,對擷取印表機的相關屬性基本是無能為力的。

C#裡面擷取印表機的詳細資料,常用的用2種方式:

  1. 使用 Windows API
  2. 使用 WMI

我這裡使用的是WMI的方式,因為此方式,是採用了類SQL的方法,將windows的WMI管理資訊,作為一種資料庫的形態來提供的,使用起來比較順手

.NET 裡面對WMI的使用,是放在 System.Management 這個空間下的,要使用的話,需要先添加對 System.Management.dll 引用

具體代碼如下:

 

 

string wmiSQL = "SELECT * FROM Win32_Printer";ManagementObjectCollection printers = new ManagementObjectSearcher(wmiSQL).Get();foreach (ManagementObject printer in printers){    PropertyDataCollection.PropertyDataEnumerator pde = printer.Properties.GetEnumerator();    while (pde.MoveNext())    {        MessageBox.Show(pde.Current.Name + " : " + pde.Current.Value);        //顯示的是 屬性名稱 : 屬性值 的形式    }}

應該是一目瞭然了吧,嘿嘿

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.