views:

9109

answers:

4

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?

+1  A: 

It's been a long time since I've worked in a Windows environment, but I would suggest that you look at using WMI.

dowski
+1  A: 

Look at PrinterSettings.InstalledPrinters

John Sheehan
+2  A: 

This should work.

using System.Drawing.Printing;

...

PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "The printer name"; // Load the appropriate printer's setting

After that, the various properties of PrinterSettings can be read.

Note that ps.isValid() can see if the printer actually exists.

Edit: One additional comment. Microsoft recommends you use a PrintDocument and modify its PrinterSettings rather than creating a PrinterSettings directly.

R. Bemrose
+7  A: 

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);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();

foreach (ManagementObject printer in coll)
{
    foreach (PropertyData property in printer.Properties)
    {
        Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
    }
}
Panos
This worked, I was able to find and read all the properties I needed. Thanks!
Nick Gotch