¿Cómo puedo verificar si una impresora está instalada y lista usando C #?

¿Cómo compruebo programáticamente si una impresora está instalada o no (y si hay una, cómo puedo verificar si está encendida y lista para usar?) En C # con .NET 3.5 y Visual Studio 2008?

Gracias por adelantado,

Este fragmento recuperará información sobre las impresoras instaladas:

using System.Drawing.Printing; //... foreach (string printerName in PrinterSettings.InstalledPrinters) { // Display the printer name. Console.WriteLine("Printer: {0}", printerName); // Retrieve the printer settings. PrinterSettings printer = new PrinterSettings(); printer.PrinterName = printerName; // Check that this is a valid printer. // (This step might be required if you read the printer name // from a user-supplied value or a registry or configuration file // setting.) if (printer.IsValid) { // Display the list of valid resolutions. Console.WriteLine("Supported Resolutions:"); foreach (PrinterResolution resolution in printer.PrinterResolutions) { Console.WriteLine(" {0}", resolution); } Console.WriteLine(); // Display the list of valid paper sizes. Console.WriteLine("Supported Paper Sizes:"); foreach (PaperSize size in printer.PaperSizes) { if (Enum.IsDefined(size.Kind.GetType(), size.Kind)) { Console.WriteLine(" {0}", size); } } Console.WriteLine(); } } 

La otra opción es usar WMI. Haga clic derecho en Proyecto> Agregar referencia> Seleccione la pestaña .NET> System.Management

 using System.Management; // ... private List GetPrinters() { List printerNames = new List(); // Use the ObjectQuery to get the list of configured printers System.Management.ObjectQuery oquery = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer"); System.Management.ManagementObjectSearcher mosearcher = new System.Management.ManagementObjectSearcher(oquery); System.Management.ManagementObjectCollection moc = mosearcher.Get(); foreach (ManagementObject mo in moc) { System.Management.PropertyDataCollection pdc = mo.Properties; foreach (System.Management.PropertyData pd in pdc) { if ((bool)mo["Network"]) { printerNames.Add(mo[pd.Name]); } } } return printerNames; } 

Aquí hay otro fragmento que muestra más propiedades:

  static void PrintProps(ManagementObject o, string prop) { try { Console.WriteLine(prop + "|" + o[prop]); } catch (Exception e) { Console.Write(e.ToString()); } } static void Main(string[] args) { ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"); foreach (ManagementObject printer in searcher.Get()) { string printerName = printer["Name"].ToString().ToLower(); Console.WriteLine("Printer :" + printerName); PrintProps(printer, "Caption"); PrintProps(printer, "ExtendedPrinterStatus"); PrintProps(printer, "Availability"); PrintProps(printer, "Default"); PrintProps(printer, "DetectedErrorState"); PrintProps(printer, "ExtendedDetectedErrorState"); PrintProps(printer, "ExtendedPrinterStatus"); PrintProps(printer, "LastErrorCode"); PrintProps(printer, "PrinterState"); PrintProps(printer, "PrinterStatus"); PrintProps(printer, "Status"); PrintProps(printer, "WorkOffline"); PrintProps(printer, "Local"); } }