Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Talking to a printer

Tags:

c#

printing

Is there a way to write some code that can 'talk' to printer in order to get some basic info about it's status? What I'm really interested in doing is finding out if it has run out of paper or has a paper jam - things of that nature. Should I use System.Management library for this type of stuff?

PS - It would also be handy to know how to get a hold of all the printers that have been set up on a particular PC. How would you go about that?

like image 356
Vidar Avatar asked May 20 '09 13:05

Vidar


People also ask

Why is my printer not connecting?

Remove and Re-Add Your Printer in Windows If this happens, go to Settings > Devices > Printers & Scanners, select your printer, and remove it. Then click Add a Printer or Scanner to re-add it to the device list. Incredibly (and frustratingly), this often gets things up and running again.

How does a printer communicate?

Communication between the printer and the computer or network is established through either a Bluetooth or Wi-Fi connection. In either case, communication is managed through the wireless transceiver units inside the printer, computer or network device.


1 Answers

Getting information from Printers using System.Management is relatively easy.

    //Declare WMI Variables
    ManagementObject MgmtObject;
    ManagementObjectCollection MgmtCollection;
    ManagementObjectSearcher MgmtSearcher;

    //Perform the search for printers and return the listing as a collection
    MgmtSearcher = new ManagementObjectSearcher("Select * from Win32_Printer");
    MgmtCollection = MgmtSearcher.Get();

    foreach (ManagementObject objWMI in MgmtCollection)
    {
       //Do whatever action you want with the Printer
    }

Look at http://msdn.microsoft.com/en-us/library/aa394363.aspx for methods and properties of Win32_Printer. For your question:

//Test whether a Win32_Printer is out of paper or jammed
int state = Int32.Parse(objWMI["PrinterState"]);
if (state == 4) {
   //Paper Jam
} else if (state == 5) {
   //Paper Out
}
like image 150
RobV Avatar answered Nov 01 '22 03:11

RobV