■ WMI를 이용해 프린터 정보 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing.Printing;

using System.Management;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 프린터 스테이트 배열

        /// </summary>

        private string[] printerStateArray =

        {

            "Paused",

            "Error",

            "Pending Deletion",

            "Paper Jam",

            "Paper Out",

            "Manual Feed",

            "Paper Problem",

            "Offline",

            "IO 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"

        };

 

        /// <summary>

        /// 프린터 상태 배열

        /// </summary>

        private string[] printerStatusArray =

        {

            "Other",

            "Unknown",

            "Idle",

            "Printing",

            "WarmUp",

            "Stopped Printing",

            "Offline"

        };

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                                      += Form_Load;

            this.printerComboBox.SelectedIndexChanged += printerComboBox_SelectedIndexChanged;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            foreach(string printer in PrinterSettings.InstalledPrinters)

            {

                this.printerComboBox.Items.Add(printer);

            }

 

            if(this.printerComboBox.Items.Count > 0)

            {

                this.printerComboBox.SelectedIndex = 0;

            }

        }

 

        #endregion

        #region 프린터 콤보 박스 선택 인덱스 변경시 처리하기 - printerComboBox_SelectedIndexChanged(sender, e)

 

        /// <summary>

        /// 프린터 콤보 박스 선택 인덱스 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void printerComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            string query = "SELECT * FROM Win32_Printer WHERE Name='" + printerComboBox.SelectedItem.ToString() + "'";

 

            ManagementObjectSearcher mso = new ManagementObjectSearcher(query);

 

            foreach(ManagementObject mo in mso.Get())

            {

                this.nameTextBox.Text = mo.Properties["Name"].Value.ToString();

 

                uint state = (uint)mo.Properties["PrinterState"].Value;

 

                this.stateTextBox.Text = this.printerStateArray[state];

 

                ushort status = (ushort)mo.Properties["PrinterStatus"].Value;

 

                this.statusTextBox.Text = this.printerStatusArray[status];

 

                this.descriptionTextBox.Text = GetPropertyValue(mo.Properties["Description"]);

 

                this.defaultTextBox.Text = GetPropertyValue(mo.Properties["Default"]);

 

                this.horizontalResolutionTextBox.Text = GetPropertyValue(mo.Properties["HorizontalResolution"]);

 

                this.verticalResolutionTextBox.Text = GetPropertyValue(mo.Properties["VerticalResolution"]);

 

                this.portTextBox.Text = GetPropertyValue(mo.Properties["PortName"]);

 

                this.paperSizeListBox.Items.Clear();

 

                string[] paperSizeArray = (string[])mo.Properties["PrinterPaperNames"].Value;

 

                foreach(string paperSize in paperSizeArray)

                {

                    this.paperSizeListBox.Items.Add(paperSize);

                }

 

                foreach(PropertyData propertyData in mo.Properties)

                {

                    string name = propertyData.Name;

 

                    if(propertyData.Value != null)

                    {

                        name += " : " + propertyData.Value.ToString();

                    }

 

                    Console.WriteLine(name);

                }

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 속성 값 구하기 - GetPropertyValue(propertyData)

 

        /// <summary>

        /// 속성 값 구하기

        /// </summary>

        /// <param name="propertyData">속성 데이터</param>

        /// <returns>속성 값</returns>

        private string GetPropertyValue(PropertyData propertyData)

        {

            if((propertyData == null) || (propertyData.Value == null))

            {

                return string.Empty;

            }

 

            return propertyData.Value.ToString();

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요