Adrian
Accessing Printer Status with WMI (c#)?
By Adrian in C# on May 13 2010
  • Adrian
    May, 2010 13

    Hi there,

    I need to write a program that will be able to determine when the printer is running low on paper and its status etc.
    I currently have the following code but for some reason the state being returned is always 0. If the printer is off, on or whatever, it is returning only a 0. This WMI stuff is a little new to me so there may be a mistake that I cannot see, help would be appreciated. The printer I am trying to access is a POS printer that prints a receipt on purchasing credit for your mobile phone.

    I know this is not the full correct code, but I would just like it to work so that I can understand it a little better and move on to what I need to do.

    Thanks

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Management;

    namespace printerWMITestAgain

    {

    class Program

    {

    static void Main(string[] args)

    {

    string status;

    //Connection to WMI object

    ConnectionOptions oConn = new ConnectionOptions();

    ManagementScope oMs = new ManagementScope("\\\\EDREN-THINK\\root\\cimv2", oConn);

    oMs.Connect();

    //Query statement

    ObjectQuery oQuery = new ObjectQuery("select PrinterState from Win32_Printer where Name =\"EPSON EU-T300 Receipt\"");

    //Execute

    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);

    //Get Results

    ManagementObjectCollection oReturnCollection = oSearcher.Get();

    //Loop Through

    foreach (ManagementObject oReturn in oReturnCollection)

    {

    status = oReturn["PrinterState"].ToString();

    Console.WriteLine(status);

    Console.ReadLine();

    }

    }

    }

    }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS