Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Check printer status in .net

Posted by Viral Sarvaiya on August 1, 2011

Hi

Today i get new thing from my current projet, how to check the status of the printer means wether the printer is connected to PC or not.

For that we have to add reference of the “System.Management”

Here it is code for check status of the printer.


using System.Management;

class PrinterOffline
{
private 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("Name_Of_Printer")) {
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.");
}
}
}
}
}

 

thanks and enjoy coding…

Advertisements

6 Responses to “Check printer status in .net”

  1. http://kellyschronicles.wordpress.com/2008/10/09/check-printer-status-with-vb-net

    vb.net version of the same code…..

    • viralsarvaiya said

      Dear Kelly’s Chronicles,

      I know that, I have copy your post to my blog. My blog is totally for my personal use . its helps me in future so i use your code to my personal blog.

      hope u have dont mind for that.

      thanks a lot for wonderful code.

  2. That’s fine. Code is code. Glad I could help. God bless and good luck with your blog.

  3. […] Morning! I wanted to give a shout out to another blogger who linked to us yesterday here. It was so nice of him you know? Just amazing, the generosity of some folks out […]

  4. Trent said

    The “printer” word is throwing an error starting after the foreach statement.

    Method Name expected.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: