How to get Printer Info in .NET?-Collection of common programming errors


  • John Saunders

    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?


  • Panos

    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);
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    ManagementObjectCollection coll = searcher.Get();
    
    foreach (ManagementObject printer in coll)
    {
        foreach (PropertyData property in printer.Properties)
        {
            Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
        }
    }
    

  • Powerlord

    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.


  • itsho

    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“.


  • David

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

    usage: printer.Properties["PropName"].Value
    

  • dowski

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


  • MarkMiddlemist