How to

enumerate available printers

Published: 8. February 2012 | Updated: 8. February 2012
License: Microsoft Public License (MS-PL)
Categories: Windows » Hardware
Tags: C# Hardware Windows WMI
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at analyzant.com

Import namespaces

using System;
using System.Collections.Generic;
using System.Management; // requires adding System.Management reference to project

Code

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
ManagementObjectCollection collection = searcher.Get();

var items = new List<Win32_Printer>();
foreach (ManagementObject obj in collection)
{
    var item = new Win32_Printer();
    item.Attributes = (uint?)obj["Attributes"];
    item.Availability = (ushort?)obj["Availability"];
    item.AvailableJobSheets = (string[])obj["AvailableJobSheets"];
    item.AveragePagesPerMinute = (uint?)obj["AveragePagesPerMinute"];
    item.Capabilities = (ushort[])obj["Capabilities"];
    item.CapabilityDescriptions = (string[])obj["CapabilityDescriptions"];
    item.Caption = (string)obj["Caption"];
    item.CharSetsSupported = (string[])obj["CharSetsSupported"];
    item.Comment = (string)obj["Comment"];
    item.ConfigManagerErrorCode = (uint?)obj["ConfigManagerErrorCode"];
    item.ConfigManagerUserConfig = (bool?)obj["ConfigManagerUserConfig"];
    item.CreationClassName = (string)obj["CreationClassName"];
    item.CurrentCapabilities = (ushort[])obj["CurrentCapabilities"];
    item.CurrentCharSet = (string)obj["CurrentCharSet"];
    item.CurrentLanguage = (ushort?)obj["CurrentLanguage"];
    item.CurrentMimeType = (string)obj["CurrentMimeType"];
    item.CurrentNaturalLanguage = (string)obj["CurrentNaturalLanguage"];
    item.CurrentPaperType = (string)obj["CurrentPaperType"];
    item.Default = (bool?)obj["Default"];
    item.DefaultCapabilities = (ushort[])obj["DefaultCapabilities"];
    item.DefaultCopies = (uint?)obj["DefaultCopies"];
    item.DefaultLanguage = (ushort?)obj["DefaultLanguage"];
    item.DefaultMimeType = (string)obj["DefaultMimeType"];
    item.DefaultNumberUp = (uint?)obj["DefaultNumberUp"];
    item.DefaultPaperType = (string)obj["DefaultPaperType"];
    item.DefaultPriority = (uint?)obj["DefaultPriority"];
    item.Description = (string)obj["Description"];
    item.DetectedErrorState = (ushort?)obj["DetectedErrorState"];
    item.DeviceID = (string)obj["DeviceID"];
    item.Direct = (bool?)obj["Direct"];
    item.DoCompleteFirst = (bool?)obj["DoCompleteFirst"];
    item.DriverName = (string)obj["DriverName"];
    item.EnableBIDI = (bool?)obj["EnableBIDI"];
    item.EnableDevQueryPrint = (bool?)obj["EnableDevQueryPrint"];
    item.ErrorCleared = (bool?)obj["ErrorCleared"];
    item.ErrorDescription = (string)obj["ErrorDescription"];
    item.ErrorInformation = (string[])obj["ErrorInformation"];
    item.ExtendedDetectedErrorState = (ushort?)obj["ExtendedDetectedErrorState"];
    item.ExtendedPrinterStatus = (ushort?)obj["ExtendedPrinterStatus"];
    item.Hidden = (bool?)obj["Hidden"];
    item.HorizontalResolution = (uint?)obj["HorizontalResolution"];
    item.InstallDate = (DateTime?)obj["InstallDate"];
    item.JobCountSinceLastReset = (uint?)obj["JobCountSinceLastReset"];
    item.KeepPrintedJobs = (bool?)obj["KeepPrintedJobs"];
    item.LanguagesSupported = (ushort[])obj["LanguagesSupported"];
    item.LastErrorCode = (uint?)obj["LastErrorCode"];
    item.Local = (bool?)obj["Local"];
    item.Location = (string)obj["Location"];
    item.MarkingTechnology = (ushort?)obj["MarkingTechnology"];
    item.MaxCopies = (uint?)obj["MaxCopies"];
    item.MaxNumberUp = (uint?)obj["MaxNumberUp"];
    item.MaxSizeSupported = (uint?)obj["MaxSizeSupported"];
    item.MimeTypesSupported = (string[])obj["MimeTypesSupported"];
    item.Name = (string)obj["Name"];
    item.NaturalLanguagesSupported = (string[])obj["NaturalLanguagesSupported"];
    item.Network = (bool?)obj["Network"];
    item.PaperSizesSupported = (ushort[])obj["PaperSizesSupported"];
    item.PaperTypesAvailable = (string[])obj["PaperTypesAvailable"];
    item.Parameters = (string)obj["Parameters"];
    item.PNPDeviceID = (string)obj["PNPDeviceID"];
    item.PortName = (string)obj["PortName"];
    item.PowerManagementCapabilities = (ushort[])obj["PowerManagementCapabilities"];
    item.PowerManagementSupported = (bool?)obj["PowerManagementSupported"];
    item.PrinterPaperNames = (string[])obj["PrinterPaperNames"];
    item.PrinterState = (uint?)obj["PrinterState"];
    item.PrinterStatus = (ushort?)obj["PrinterStatus"];
    item.PrintJobDataType = (string)obj["PrintJobDataType"];
    item.PrintProcessor = (string)obj["PrintProcessor"];
    item.Priority = (uint?)obj["Priority"];
    item.Published = (bool?)obj["Published"];
    item.Queued = (bool?)obj["Queued"];
    item.RawOnly = (bool?)obj["RawOnly"];
    item.SeparatorFile = (string)obj["SeparatorFile"];
    item.ServerName = (string)obj["ServerName"];
    item.Shared = (bool?)obj["Shared"];
    item.ShareName = (string)obj["ShareName"];
    item.SpoolEnabled = (bool?)obj["SpoolEnabled"];
    item.StartTime = (DateTime?)obj["StartTime"];
    item.Status = (string)obj["Status"];
    item.StatusInfo = (ushort?)obj["StatusInfo"];
    item.SystemCreationClassName = (string)obj["SystemCreationClassName"];
    item.SystemName = (string)obj["SystemName"];
    item.TimeOfLastReset = (DateTime?)obj["TimeOfLastReset"];
    item.UntilTime = (DateTime?)obj["UntilTime"];
    item.VerticalResolution = (uint?)obj["VerticalResolution"];
    item.WorkOffline = (bool?)obj["WorkOffline"];

    items.Add(item);
}

Class

public class Win32_Printer
{
    public uint? Attributes;
    public ushort? Availability;
    public string[] AvailableJobSheets;
    public uint? AveragePagesPerMinute;
    public ushort[] Capabilities;
    public string[] CapabilityDescriptions;
    public string Caption;
    public string[] CharSetsSupported;
    public string Comment;
    public uint? ConfigManagerErrorCode;
    public bool? ConfigManagerUserConfig;
    public string CreationClassName;
    public ushort[] CurrentCapabilities;
    public string CurrentCharSet;
    public ushort? CurrentLanguage;
    public string CurrentMimeType;
    public string CurrentNaturalLanguage;
    public string CurrentPaperType;
    public bool? Default;
    public ushort[] DefaultCapabilities;
    public uint? DefaultCopies;
    public ushort? DefaultLanguage;
    public string DefaultMimeType;
    public uint? DefaultNumberUp;
    public string DefaultPaperType;
    public uint? DefaultPriority;
    public string Description;
    public ushort? DetectedErrorState;
    public string DeviceID;
    public bool? Direct;
    public bool? DoCompleteFirst;
    public string DriverName;
    public bool? EnableBIDI;
    public bool? EnableDevQueryPrint;
    public bool? ErrorCleared;
    public string ErrorDescription;
    public string[] ErrorInformation;
    public ushort? ExtendedDetectedErrorState;
    public ushort? ExtendedPrinterStatus;
    public bool? Hidden;
    public uint? HorizontalResolution;
    public DateTime? InstallDate;
    public uint? JobCountSinceLastReset;
    public bool? KeepPrintedJobs;
    public ushort[] LanguagesSupported;
    public uint? LastErrorCode;
    public bool? Local;
    public string Location;
    public ushort? MarkingTechnology;
    public uint? MaxCopies;
    public uint? MaxNumberUp;
    public uint? MaxSizeSupported;
    public string[] MimeTypesSupported;
    public string Name;
    public string[] NaturalLanguagesSupported;
    public bool? Network;
    public ushort[] PaperSizesSupported;
    public string[] PaperTypesAvailable;
    public string Parameters;
    public string PNPDeviceID;
    public string PortName;
    public ushort[] PowerManagementCapabilities;
    public bool? PowerManagementSupported;
    public string[] PrinterPaperNames;
    public uint? PrinterState;
    public ushort? PrinterStatus;
    public string PrintJobDataType;
    public string PrintProcessor;
    public uint? Priority;
    public bool? Published;
    public bool? Queued;
    public bool? RawOnly;
    public string SeparatorFile;
    public string ServerName;
    public bool? Shared;
    public string ShareName;
    public bool? SpoolEnabled;
    public DateTime? StartTime;
    public string Status;
    public ushort? StatusInfo;
    public string SystemCreationClassName;
    public string SystemName;
    public DateTime? TimeOfLastReset;
    public DateTime? UntilTime;
    public uint? VerticalResolution;
    public bool? WorkOffline;
}
Send us feedback about this snippet »



Related Snippets: