Search results for 'ManagementObject'. 1 post(s) found.

  1. 2007/08/28 How To Check If Your Printer Is Connected Using C#
2007/08/28 08:00

How To Check If Your Printer Is Connected Using C#


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(@"\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 PnP Printer is not connected.");
     }
     else
     {
      // Printer is not offline
       Console.WriteLine("Your PnP Printer is connected.");
     }
    }
   }
  }
 }
}

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 03:35 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 12:25 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:44 delete

    moneyideas