17 February 2014

Show a list of Network and Local Printers using .Net Framework in C#.net



using System;
using System.Windows.Forms;
using System.Management;

private void PrinterList()
{
    // USING WMI. (WINDOWS MANAGEMENT INSTRUMENTATION)

    System.Management.ManagementScope objMS =
        new System.Management.ManagementScope(ManagementPath.DefaultPath);
    objMS.Connect();

    SelectQuery objQuery = new SelectQuery("SELECT * FROM Win32_Printer");
    ManagementObjectSearcher objMOS = new ManagementObjectSearcher(objMS, objQuery);
    System.Management.ManagementObjectCollection objMOC = objMOS.Get();
    foreach (ManagementObject Printers in objMOC)
    {
        if (Convert.ToBoolean(Printers["Local"]))       // LOCAL PRINTERS.
        {
            cmbLocalPrinters.Items.Add(Printers["Name"]);
        }
        if (Convert.ToBoolean(Printers["Network"]))     // ALL NETWORK PRINTERS.
        {
            cmbNetworkPrinters.Items.Add(Printers["Name"]);
        }
    }
}

No comments:

Post a Comment