How to get printer information in .NET?

There are four values ​​associated with the selected printer in the standard PrintDialog: Status, Type, Where and Comment.

If I know the name of the printer, how can I get these values ​​in C # 2.0?

+44
c # printing printers printdialog
Nov 17 '08 at 17:05
source share
8 answers

As dowski suggested , you can use WMI to get printer properties. The following code displays all the properties for a given printer name. Among them you will find: PrinterStatus, Comment, Location, DriverName, PortName, etc.

using System.Management; 

...

 string printerName = "YourPrinterName"; string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) using (ManagementObjectCollection coll = searcher.Get()) { try { foreach (ManagementObject printer in coll) { foreach (PropertyData property in printer.Properties) { Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value)); } } } catch (ManagementException ex) { Console.WriteLine(ex.Message); } } 
+62
Nov 17 '08 at 18:11
source share

That should work.

 using System.Drawing.Printing; 

...

 PrinterSettings ps = new PrinterSettings(); ps.PrinterName = "The printer name"; // Load the appropriate printer setting 

After that, you can read the various properties of PrinterSettings.

Note that ps.isValid() can see if the printer really exists.

Edit: Another comment. Microsoft recommends using PrintDocument and changing its PrinterSettings settings, rather than directly creating PrinterSettings settings.

+22
Nov 17 '08 at 17:42
source share
+6
Nov 17 '08 at 17:32
source share

Note that the article in which dowski and Panos pointed out ( MSDN Win32_Printer ) may be a little misleading.

I mean the first value of most arrays. some start with 1, and some start with 0. for example, the first value of "ExtendedPrinterStatus" in the table is 1, so your array should look something like this:

 string[] arrExtendedPrinterStatus = { "","Other", "Unknown", "Idle", "Printing", "Warming Up", "Stopped Printing", "Offline", "Paused", "Error", "Busy", "Not Available", "Waiting", "Processing", "Initialization", "Power Save", "Pending Deletion", "I/O Active", "Manual Feed" }; 

and on the other hand, the first ErrorState value in the table is 0, so your array should be something like this:

 string[] arrErrorState = { "Unknown", "Other", "No Error", "Low Paper", "No Paper", "Low Toner", "No Toner", "Door Open", "Jammed", "Offline", "Service Requested", "Output Bin Full" }; 

BTW, "PrinterState" is deprecated, but you can use "PrinterStatus".

+3
Aug 14 '11 at 19:51
source share

It has been a long time since I worked in a Windows environment, but I would suggest you look using WMI .

+2
Nov 17 '08 at 17:23
source share

For reference, here is a list of all available properties for a PolicyObject.

 usage: printer.Properties["PropName"].Value 
+2
Jan 13 '12 at 21:32
source share

I know this is an old publication, but it is now easier or faster to use the advanced printing services offered by the WPF infrastructure (you can use applications other than WPF).

http://msdn.microsoft.com/en-us/library/System.Printing(v=vs.110).aspx

Example for getting the status of the printer queue and the first job.

 var queue = new LocalPrintServer().GetPrintQueue("Printer Name"); var queueStatus = queue.QueueStatus; var jobStatus = queue.GetPrintJobInfoCollection().FirstOrDefault().JobStatus 
+2
Nov 04 '14 at 15:04
source share

As an alternative to WMI, you can get fast, accurate results by clicking on WinSpool.drv (for example, the Windows API) - you can get all the details about the interfaces, structures and constants from pinvoke.net, or I put the code together http: // delradiesdev. blogspot.com/2012/02/accessing-printer-status-using-winspool.html

0
Feb 27 2018-12-12T00:
source share



All Articles