Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Printer Info in .NET?

In the standard PrintDialog there are four values associated with a selected printer: Status, Type, Where, and Comment.

If I know a printer's name, how can I get these values in C# 2.0?

like image 568
Nick Gotch Avatar asked Nov 17 '08 17:11

Nick Gotch


People also ask

How do I find the IP address of my printer in C#?

I had to change code to String IP = (String)key. GetValue("HostName", String. Empty, RegistryValueOptions. DoNotExpandEnvironmentNames); for my requirement.


1 Answers

As dowski suggested, you could use WMI to get printer properties. The following code displays all properties for a given printer name. Among them you will find: PrinterStatus, Comment, Location, DriverName, PortName, etc.

using System.Management; 

...

string printerName = "YourPrinterName"; string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);  using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) using (ManagementObjectCollection coll = searcher.Get()) {     try     {         foreach (ManagementObject printer in coll)         {             foreach (PropertyData property in printer.Properties)             {                 Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));             }         }     }     catch (ManagementException ex)     {         Console.WriteLine(ex.Message);     } } 
like image 85
Panos Avatar answered Sep 30 '22 09:09

Panos