Click here to Skip to main content
6,630,586 members and growing! (15,239 online)
Email Password   helpLost your password?
Desktop Development » Printing » General     Intermediate

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

By zeddy

Make sure you check your printer's offline status before printing. This article shows how to do just that.
C#, .NET, Win2K, WinXP, Win2003, Visual Studio, Dev
Posted:10 Feb 2004
Views:74,268
Bookmarked:48 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 3.25 Rating: 2.77 out of 5
4 votes, 26.7%
1
2 votes, 13.3%
2
1 vote, 6.7%
3
2 votes, 13.3%
4
6 votes, 40.0%
5

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

About the Author

zeddy


Member
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
Occupation: Web Developer
Location: Indonesia Indonesia

Other popular Printing articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralBrilliant, got my 5 Pinmembercaptainplanet012311:51 30 Apr '09  
Generalprinter on C#.net Pinmemberbalu123458:33 30 Jul '08  
QuestionNot working for the first time Pinmembersrinivas@mtccrm.com4:56 13 Jun '08  
QuestionBetter Solution PinmemberArjan Mels13:02 19 Mar '07  
QuestionHow can do the same in VB or VC++ Pinmembervijay kumar T11:27 19 Jan '06  
AnswerRe: How can do the same in VB or VC++ PinmemberChristian Graus12:21 19 Jan '06  
GeneralRe: How can do the same in VB or VC++ Pinmembervijay kumar T12:51 19 Jan '06  
GeneralRe: How can do the same in VB or VC++ PinmemberChristian Graus12:56 19 Jan '06  
GeneralSame code using PrinterStatus Pinmembercseils21:20 22 Nov '04  
GeneralRe: Same code using PrinterStatus PinmvpJohn Simmons / outlaw programmer1:20 11 Dec '08  
GeneralCode does not perform as stated. Pinmemberamurray@unumprovident.com8:32 30 Aug '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Feb 2004
Editor: Smitha Vijayan
Copyright 2004 by zeddy
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project