Click here to Skip to main content
11,639,554 members (61,692 online)
Click here to Skip to main content

How to Check if your Printer is Connected, using C#

, 10 Feb 2004 142.8K 2 68
Rate this:
Please Sign up or sign in to vote.
Make sure you check your printer's offline status before printing. This article shows how to do just that.

Introduction

This code will test whether a printer is connected to your system or not. It works with USB and network printers (haven't tested LPT printers yet).

Some printers (like my HP DeskJet 930c) is detected online even though I press the OFF button (as long as the power adaptor is switched ON). This is true, as the printer is able to switch on itself when the print queue is not empty. But if I switch off the power adaptor, then the printer cannot switch on itself, and Windows automatically adjusts the printer setting by checking the "Use Printer Offline" in the Printer Control Panel.

Same case with network printers. When I plug my network cable, Windows automatically unchecks the "Use Printer Offline", and when I unplug it, Windows rechecks the "Use Printer Offline". This is what my code detects, whether "Use Printer Offline" is actually checked or not.

using System;
using System.Management; 
namespace zedilabs.com
{
 class PrinterOffline
 {
  [STAThread]
  static void Main(string[] args)
  {
   // Set management scope
   ManagementScope scope = new ManagementScope(@"\root\cimv2");
   scope.Connect();

   // Select Printers from WMI Object Collections
   ManagementObjectSearcher searcher = new 
    ManagementObjectSearcher("SELECT * FROM Win32_Printer");

   string printerName = "";
   foreach (ManagementObject printer in searcher.Get()) 
   {
    printerName = printer["Name"].ToString().ToLower();
    if (printerName.Equals(@"hp deskjet 930c"))
    {
     Console.WriteLine("Printer = " + printer["Name"]); 
     if (printer["WorkOffline"].ToString().ToLower().Equals("true"))
     {
      // printer is offline by user
      Console.WriteLine("Your Plug-N-Play printer is not connected.");
     }
     else
     {
      // printer is not offline
       Console.WriteLine("Your Plug-N-Play printer is connected.");
     }
    }
   }
  }
 }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

zeddy
Web Developer
Indonesia Indonesia
Zeddy Iskandar, computer science student. Writes freewares on free time. Regularly rambles on his blog. Likes to experiment on emerging software technologies.

www.zedilabs.com

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Sibeesh Venu8-Aug-14 1:29
professionalSibeesh Venu8-Aug-14 1:29 
GeneralMy vote of 1 Pin
behzad200026-Apr-10 9:56
memberbehzad200026-Apr-10 9:56 
GeneralBrilliant, got my 5 Pin
captainplanet012330-Apr-09 10:51
membercaptainplanet012330-Apr-09 10:51 
Generalprinter on C#.net Pin
balu1234530-Jul-08 7:33
memberbalu1234530-Jul-08 7:33 
QuestionNot working for the first time Pin
srinivas@mtccrm.com13-Jun-08 3:56
membersrinivas@mtccrm.com13-Jun-08 3:56 
QuestionBetter Solution Pin
Arjan Mels19-Mar-07 12:02
memberArjan Mels19-Mar-07 12:02 
QuestionHow can do the same in VB or VC++ Pin
vijay kumar T19-Jan-06 10:27
membervijay kumar T19-Jan-06 10:27 
AnswerRe: How can do the same in VB or VC++ Pin
Christian Graus19-Jan-06 11:21
memberChristian Graus19-Jan-06 11:21 
GeneralRe: How can do the same in VB or VC++ Pin
vijay kumar T19-Jan-06 11:51
membervijay kumar T19-Jan-06 11:51 
GeneralRe: How can do the same in VB or VC++ Pin
Christian Graus19-Jan-06 11:56
memberChristian Graus19-Jan-06 11:56 
GeneralSame code using PrinterStatus Pin
cseils22-Nov-04 20:20
membercseils22-Nov-04 20:20 
GeneralRe: Same code using PrinterStatus Pin
John Simmons / outlaw programmer11-Dec-08 0:20
mvpJohn Simmons / outlaw programmer11-Dec-08 0:20 
GeneralCode does not perform as stated. Pin
amurray@unumprovident.com30-Aug-04 7:32
memberamurray@unumprovident.com30-Aug-04 7:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 11 Feb 2004
Article Copyright 2004 by zeddy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid