How to get Printer Info in .NET?
Asked Answered
A

8

50

In the standard PrintDialog there are four values associated with a selected printer: Status, Type, Where, and Comment.

If I know a printer's name, how can I get these values in C# 2.0?

Academia answered 17/11, 2008 at 17:5 Comment(0)
C
74

As dowski suggested, you could use WMI to get printer properties. The following code displays all 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);
    }
}
Chenab answered 17/11, 2008 at 18:11 Comment(0)
B
25

This should work.

using System.Drawing.Printing;

...

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

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

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

Edit: One additional comment. Microsoft recommends you use a PrintDocument and modify its PrinterSettings rather than creating a PrinterSettings directly.

Beatup answered 17/11, 2008 at 17:42 Comment(1)
This doesn't work. In my case it doesn't update the properties (for example CanDuplex). Also, the documentation says that IsDefaultPrinter "Gets a value indicating whether the PrinterName property designates the default printer, except when the user explicitly sets PrinterName."Bays
S
6

Look at PrinterSettings.InstalledPrinters

Scrimpy answered 17/11, 2008 at 17:32 Comment(0)
A
4

Just for reference, here is a list of all the available properties for a printer ManagementObject.

usage: printer.Properties["PropName"].Value
Azaria answered 13/1, 2012 at 21:32 Comment(0)
A
3

Please notice that the article that dowski and Panos was reffering to (MSDN Win32_Printer) can be a little misleading.

I'm referring the first value of most of the arrays. some begins with 1 and some begins with 0. for example, "ExtendedPrinterStatus" first value in table is 1, therefore, your array should be 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, "ErrorState" first value in table is 0, therefore, 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 obsolete, but you can use "PrinterStatus".

Anaesthesiology answered 14/8, 2011 at 19:51 Comment(0)
W
2

It's been a long time since I've worked in a Windows environment, but I would suggest that you look at using WMI.

Wideranging answered 17/11, 2008 at 17:23 Comment(0)
I
2

I know it's an old posting, but nowadays the easier/quicker option is to use the enhanced printing services offered by the WPF framework (usable by non-WPF apps).

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

An example to retrieve the status of the printer queue and first job..

var queue = new LocalPrintServer().GetPrintQueue("Printer Name");
var queueStatus = queue.QueueStatus;
var jobStatus = queue.GetPrintJobInfoCollection().FirstOrDefault().JobStatus
Idomeneus answered 4/11, 2014 at 15:4 Comment(1)
Both LocalPrintServer and the queue returned from GetPrintQueue are IDisposable so you should really wrap that in a using call. using (var lps = new LocalPrintServer()) { using (var queue = lps.GetPrintQueue(printerName)) { var queueStatus = queue.QueueStatus; var jobStatus = queue.GetPrintJobInfoCollection().FirstOrDefault().JobStatus; } }Amaty
C
0

As an alternative to WMI you can get fast accurate results by tapping in to WinSpool.drv (i.e. Windows API) - you can get all the details on the interfaces, structs & constants from pinvoke.net, or I've put the code together at http://delradiesdev.blogspot.com/2012/02/accessing-printer-status-using-winspool.html

Ca answered 27/2, 2012 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.