How To Check If Your Printer Is Connected Using C#

CK CK 3776 
Created at  
1,025 0 0

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 kurapa.com
{
  class PrinterOffline
  {
    [STAThread]
    static void Main(string[] args)
    {
      // Set management scope
      ManagementScope scope = new ManagementScope(@"ootcimv2");
      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 PnP printer is not connected.");
          }
          else
          {
            // printer is not offline
            Console.WriteLine("Your PnP printer is connected.");
          }
        }
      }
    }
  }
}
Tags C# C# Print HP DeskJet 930c Printer Windows Printer Facebook X
Comments 0
Similar posts
  1. How to get process id in C# like windows task manager ?
    865
  2. How to convert integer to text string ?
    695
  3. How to convert text string to integer ?
    608
  4. How to resize and save PNG or Jpeg Image file ?
    629
  5. How to convert bitmap image to jpeg image ?
    352
  6. To load image from file
    445
  7. How to download/grap web pages or XML document at internet website in Delphi XE?
    865
  8. How to speed up access to the RichTextBox Lines property
    825
  9. The best way to enable and disable buttons, menu items and toolbar buttons
    666
  10. How to restrict a program to a single instance
    726
  11. Graphics in C# - Irregular Forms
    904
  12. Extracting the Country from IP Address
    463
  13. How to print character in C# by ASCII code
    474
  14. Adding Custom Paper Sizes To Named Printers
    643
  15. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    971
  16. A Customizable Printing Text Class
    426
  17. Simplified .NET Printing In C#
    686
  18. How To Print Text In C#
    453
  19. How to send binary data through C# CGI app?
    992
  20. Calling Your Main Thread From A Worker Thread In C#
    544
  21. How To Read And Write A Cookie
    358
  22. Using C# With Cookies
    618
  1. Conversation Multi-Agent Chatbot with On-the-Fly Code Execition via Microsoft AutoGen
    292
  2. Getting Started with FAISS: A CentOS 7 Installation Guide
    277
  3. CMake - a cross-platform build system generator that creates Makefiles used in C++ projects
    291
  4. KMP String Matching Algorithm
    668
  5. How to get process id in C# like windows task manager ?
    865
  6. How to implement switch statement in C# ?
    983
  7. How to implement while loop in C# ?
    1,334
  8. How to delete file ?
    1,186
  9. How to convert integer to text string ?
    695
  10. How to convert text string to integer ?
    608
  11. How to set an certain color as transparent color on bitmap
    508
  12. How to resize and save PNG or Jpeg Image file ?
    629
  13. How to display message box in C#
    1,101
  14. How to convert bitmap image to jpeg image ?
    352
  15. To load image from file
    445
  16. URL Encode, Decode function for MFC
    601
  17. How to load HTML resource on MFC ?
    838
  18. How to get GM Time as String ?
    422
Recently updated
  1. Why SPF and DKIM Are Essential for Linux Email Servers?
    2
  2. How do I create animated background for Google Meet?
    19,014
  3. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    193
  4. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    75
  5. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    151
  6. Array of arguments passed to script in PHP
    83
  7. Invalid Auction Storms in Ad Tech
    91
  8. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    194
  9. The Two Clocks of Ad Server Architecture
    61
  10. Frequency Cap Architecture Overview
    505
  11. Collecting Proof-of-Play from 100,000 Screens
    139
  12. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    276
  13. What is RedisVL?
    102
  14. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    219
  15. TPU & GPU Cluster Management Tools
    229
  16. The Concept of Scaffolding in AI Agents
    217
  17. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    568
  18. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    246
  19. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    321
  20. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    196
  21. Machine Learning: Training vs. Ranking
    200
  22. The Continuous Learning Machine: Designing a Loop That Actually Turns
    212
  23. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    188
  24. Designing a Data Curation System Around a Vector Database
    212
  25. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    228
  26. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    646
  27. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    181
  28. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    203
  29. Understanding Data Lineage
    251
  30. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    159