A netizen sent a message to ask, C # How to traverse the system has installed all the printers, and get information about each printer, such as: port, name and so on
C # inside, although in the System.Drawing.Printing this namespace, provides some access to the system printer, but, to tell the truth is too weak, to obtain the relevant properties of the printer is basically powerless.
C # inside to get the details of the printer, commonly used in 2 ways:
- Using the Windows API
- Using WMI
I'm using WMI in this way, because it's a method of class SQL that provides the WMI management information of Windows as a form of a database, which is easier to use
The use of WMI in. NET is placed in the System.Management space, and if you want to use it, you need to add a reference to the System.Management.dll
The specific code is as follows:
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); Displays the property name: The form of the property value }}
It should be at a glance, hey.
C # traverses the printer installed by the system, using WMI to get all the properties of the printer