using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Drawing.Printing;
using System.Management;

// Add a reference to System.Management.
// For more information about the data available in the Win32_Printer class, see:
//      https://msdn.microsoft.com/library/aa394363.aspx

namespace PrinterInfo
{
    public partial class frm78PrinterInfo : Form
    {
        public frm78PrinterInfo()
        {
            InitializeComponent();
        }

        // List the installed printers.
        private void frm78PrinterInfo_Load(object sender, EventArgs e)
        {
            // Find all of the installed printers.
            foreach (string printer in PrinterSettings.InstalledPrinters)
                printersComboBox.Items.Add(printer);

            // Select the first printer.
            printersComboBox.SelectedIndex = 0;
        }

        // Display information about the selected printer.
        private void printersComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Lookup arrays.
            string[] PrinterStatuses =
            {
                "???", "Other", "Unknown", "Idle", "Printing", "Warming Up",
                "Stopped Printing", "Offline", "Paused", "Error", "Busy",
                "Not Available", "Waiting", "Processing", "Initialization",
                "Power Save", "Pending Deletion", "I/O Active", "Manual Feed",
            };
            string[] PrinterStates =
            {
                "Idle", "Paused", "Error", "Pending Deletion", "Paper Jam", "Paper Out",
                "Manual Feed", "Paper Problem", "Offline", "I/O Active", "Busy",
                "Printing", "Output Bin Full", "Not Available", "Waiting", "Processing",
                "Initialization", "Warming Up", "Toner Low", "No Toner", "Page Punt",
                "User Intervention Required", "Out of Memory", "Door Open",
                "Server_Unknown", "Power Save",
            };

            // Get a ManagementObjectSearcher for the printer.
            string query = "SELECT * FROM Win32_Printer WHERE Name='" +
                printersComboBox.SelectedItem.ToString() + "'";
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(query);

            // Get the ManagementObjectCollection representing
            // the result of the WMI query. Loop through its
            // single item. Display some of that item's properties.
            foreach (ManagementObject service in searcher.Get())
            {
                nameTextBox.Text = service.Properties["Name"].Value.ToString();

                UInt32 state = (UInt32)service.Properties["PrinterState"].Value;
                stateTextBox.Text = PrinterStates[state];

                UInt16 status = (UInt16)service.Properties["PrinterStatus"].Value;
                statusTextBox.Text = PrinterStatuses[status];

                descriptionTextBox.Text = GetPropertyValue(service.Properties["Description"]);
                defaultTextBox.Text = GetPropertyValue(service.Properties["Default"]);
                horResTextBox.Text = GetPropertyValue(service.Properties["HorizontalResolution"]);
                vertResTextBox.Text = GetPropertyValue(service.Properties["VerticalResolution"]);
                portTextBox.Text = GetPropertyValue(service.Properties["PortName"]);

                paperSizesListBox.Items.Clear();
                string[] paperSizes = (string[])service.Properties["PrinterPaperNames"].Value;
                foreach (string size in paperSizes) paperSizesListBox.Items.Add(size);

                // List the available properties.
                foreach (PropertyData data in service.Properties)
                {
                    string text = data.Name;
                    if (data.Value != null)
                        text += ": " + data.Value.ToString();
                    Console.WriteLine(text);
                }
            }
        }

        // If the data is not null and has a value, return it.
        private string GetPropertyValue(PropertyData data)
        {
            if ((data == null) || (data.Value == null)) return "";
            return data.Value.ToString();
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.paperSizesListBox = new System.Windows.Forms.ListBox();
            this.label9 = new System.Windows.Forms.Label();
            this.portTextBox = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.vertResTextBox = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.horResTextBox = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.defaultTextBox = new System.Windows.Forms.TextBox();
            this.label7 = new System.Windows.Forms.Label();
            this.descriptionTextBox = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.statusTextBox = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.stateTextBox = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.nameTextBox = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.printersComboBox = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // paperSizesListBox
            // 
            this.paperSizesListBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.paperSizesListBox.FormattingEnabled = true;
            this.paperSizesListBox.IntegralHeight = false;
            this.paperSizesListBox.Location = new System.Drawing.Point(84, 247);
            this.paperSizesListBox.Name = "paperSizesListBox";
            this.paperSizesListBox.Size = new System.Drawing.Size(188, 102);
            this.paperSizesListBox.TabIndex = 39;
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(12, 250);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(66, 13);
            this.label9.TabIndex = 38;
            this.label9.Text = "Paper Sizes:";
            // 
            // portTextBox
            // 
            this.portTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.portTextBox.Location = new System.Drawing.Point(84, 221);
            this.portTextBox.Name = "portTextBox";
            this.portTextBox.Size = new System.Drawing.Size(188, 20);
            this.portTextBox.TabIndex = 37;
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(12, 224);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(29, 13);
            this.label8.TabIndex = 36;
            this.label8.Text = "Port:";
            // 
            // vertResTextBox
            // 
            this.vertResTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.vertResTextBox.Location = new System.Drawing.Point(84, 195);
            this.vertResTextBox.Name = "vertResTextBox";
            this.vertResTextBox.Size = new System.Drawing.Size(188, 20);
            this.vertResTextBox.TabIndex = 35;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(12, 198);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(51, 13);
            this.label5.TabIndex = 34;
            this.label5.Text = "Vert Res:";
            // 
            // horResTextBox
            // 
            this.horResTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.horResTextBox.Location = new System.Drawing.Point(84, 169);
            this.horResTextBox.Name = "horResTextBox";
            this.horResTextBox.Size = new System.Drawing.Size(188, 20);
            this.horResTextBox.TabIndex = 33;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(12, 172);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(49, 13);
            this.label6.TabIndex = 32;
            this.label6.Text = "Hor Res:";
            // 
            // defaultTextBox
            // 
            this.defaultTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.defaultTextBox.Location = new System.Drawing.Point(84, 143);
            this.defaultTextBox.Name = "defaultTextBox";
            this.defaultTextBox.Size = new System.Drawing.Size(188, 20);
            this.defaultTextBox.TabIndex = 31;
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(12, 146);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(44, 13);
            this.label7.TabIndex = 30;
            this.label7.Text = "Default:";
            // 
            // descriptionTextBox
            // 
            this.descriptionTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.descriptionTextBox.Location = new System.Drawing.Point(84, 117);
            this.descriptionTextBox.Name = "descriptionTextBox";
            this.descriptionTextBox.Size = new System.Drawing.Size(188, 20);
            this.descriptionTextBox.TabIndex = 29;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(12, 120);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(63, 13);
            this.label4.TabIndex = 28;
            this.label4.Text = "Description:";
            // 
            // statusTextBox
            // 
            this.statusTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.statusTextBox.Location = new System.Drawing.Point(84, 91);
            this.statusTextBox.Name = "statusTextBox";
            this.statusTextBox.Size = new System.Drawing.Size(188, 20);
            this.statusTextBox.TabIndex = 27;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(12, 94);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(40, 13);
            this.label3.TabIndex = 26;
            this.label3.Text = "Status:";
            // 
            // stateTextBox
            // 
            this.stateTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.stateTextBox.Location = new System.Drawing.Point(84, 65);
            this.stateTextBox.Name = "stateTextBox";
            this.stateTextBox.Size = new System.Drawing.Size(188, 20);
            this.stateTextBox.TabIndex = 25;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 68);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(35, 13);
            this.label2.TabIndex = 24;
            this.label2.Text = "State:";
            // 
            // nameTextBox
            // 
            this.nameTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.nameTextBox.Location = new System.Drawing.Point(84, 39);
            this.nameTextBox.Name = "nameTextBox";
            this.nameTextBox.Size = new System.Drawing.Size(188, 20);
            this.nameTextBox.TabIndex = 23;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 42);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(38, 13);
            this.label1.TabIndex = 22;
            this.label1.Text = "Name:";
            // 
            // printersComboBox
            // 
            this.printersComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.printersComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.printersComboBox.FormattingEnabled = true;
            this.printersComboBox.Location = new System.Drawing.Point(12, 12);
            this.printersComboBox.Name = "printersComboBox";
            this.printersComboBox.Size = new System.Drawing.Size(260, 21);
            this.printersComboBox.TabIndex = 21;
            this.printersComboBox.SelectedIndexChanged += new System.EventHandler(this.printersComboBox_SelectedIndexChanged);
            // 
            // frm78PrinterInfo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 361);
            this.Controls.Add(this.paperSizesListBox);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.portTextBox);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.vertResTextBox);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.horResTextBox);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.defaultTextBox);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.descriptionTextBox);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.statusTextBox);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.stateTextBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.nameTextBox);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.printersComboBox);
            this.Name = "frm78PrinterInfo";
            this.Text = "PrinterInfo";
            this.Load += new System.EventHandler(this.frm78PrinterInfo_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ListBox paperSizesListBox;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.TextBox portTextBox;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.TextBox vertResTextBox;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox horResTextBox;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.TextBox defaultTextBox;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.TextBox descriptionTextBox;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox statusTextBox;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox stateTextBox;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox nameTextBox;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox printersComboBox;
    }
}