Search code examples
c#.net-4.0printingstatus

c# printer properties WMI


Hello I have this code to retreive printer properties:

string printerName = "PrinterName";
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));
    }
}

But properties I need always return the same:

PrinterState:0

PrinterStatus:3

Basically I need this to check if printer is out of paper. What I think would be: PrinterState: 4

Tested on wxp-86 and w7-64 return the same, .Net 4.0

Thank you.


Solution

  • According to msdn , Paper Out=5

    using System;
    using System.Management;
    using System.Windows.Forms;
    
    namespace WMISample
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    string printerName = "PrinterName";
                    ManagementObjectSearcher searcher = 
                        new ManagementObjectSearcher("root\\CIMV2", 
                        "SELECT * FROM Win32_Printer "
                         + "WHERE Name LIKE '%{0}'", printerName);); 
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_Printer instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("PrinterStatus: {0}", queryObj["PrinterStatus"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }