How to detect if a printer is connected in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
in c# how can one detect if a printer is connected to the computer or not.
Thanks in advance
-zs
 
Do you mean a locally attached printer or just a connection to a
network printer?

You could try using WMI (accessed via System.Management in .NET).
There's a Win32_Printer class which has a PrinterStatus field. I
gather that it doesn't work for every printer driver so you might have
to do some testing.

If you're new to WMI, get the WMI Tools (link at end) from MS. Use the
Object Browser to have a poke around.

You can get faster feedback knocking up the WMI part of your query in
script form and then converting to .NET. Go to ScriptCenter (link at
end) and get the WMI Scriptomatic. You can knock up WMI scripts and
test them very quickly and easily with this tool.

Also, this might help:
http://www.dotnet247.com/247reference/msgs/45/229078.aspx

http://www.microsoft.com/downloads/...FamilyID=6430F853-1120-48DB-8CC5-F2ABDC3ED314
http://www.microsoft.com/technet/scriptcenter/tools/scripto2.mspx
 
I used the class System.Management.ManagementObjectSearcher class as follows :

System.Management.ManagementObjectSearcher searcher =
new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_Printer");

the searcher.Get()returns printer objects even when not conneted to a
printer. It detectes all printers that are installed on my computer. and the
Printer status returns a value of Idle instead of Offline..
and when the user clicks on Print (when offline) it throws an exception. How
can I prevent this.

Can someone help me on this.
Thank you
-ZS
 
To see if a printer is connected or not you should have bidirectional
communication between computer and printer. I think the best solution
could be to retreive the status of the printer from the driver status.
I think you cannot detect a lot of old printer if they are or not
online.
 
Back
Top