How to get the list of all printers in computer

前端 未结 6 1069
长发绾君心
长发绾君心 2020-11-29 17:58

I need to get the list of all printers that connect to computer?

How I can do it in C#, WinForms?

相关标签:
6条回答
  • 2020-11-29 18:43

    If you need more information than just the name of the printer you can use the System.Management API to query them:

    var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer");
    foreach (var printer in printerQuery.Get())
    {
        var name = printer.GetPropertyValue("Name");
        var status = printer.GetPropertyValue("Status");
        var isDefault = printer.GetPropertyValue("Default");
        var isNetworkPrinter = printer.GetPropertyValue("Network");
    
        Console.WriteLine("{0} (Status: {1}, Default: {2}, Network: {3}", 
                    name, status, isDefault, isNetworkPrinter);
    }
    
    0 讨论(0)
  • 2020-11-29 18:45

    You can also use the LocalPrintServer class. See: System.Printing.LocalPrintServer

        public List<string>  InstalledPrinters
        {
            get
            {
                return (from PrintQueue printer in new LocalPrintServer().GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local,
                    EnumeratedPrintQueueTypes.Connections }).ToList()
                        select printer.Name).ToList(); 
            } 
        }
    

    As stated in the docs: Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service.

    0 讨论(0)
  • 2020-11-29 18:50

    Try this:

    foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
    {
        MessageBox.Show(printer);
    }
    
    0 讨论(0)
  • 2020-11-29 18:50

    Get Network and Local Printer List in ASP.NET

    This method uses the Windows Management Instrumentation or the WMI interface. It’s a technology used to get information about various systems (hardware) running on a Windows Operating System.

    private void GetAllPrinterList()
            {
                ManagementScope objScope = new ManagementScope(ManagementPath.DefaultPath); //For the local Access
                objScope.Connect();
    
                SelectQuery selectQuery = new SelectQuery();
                selectQuery.QueryString = "Select * from win32_Printer";
                ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
                ManagementObjectCollection MOC = MOS.Get();
                foreach (ManagementObject mo in MOC)
                {
                    lstPrinterList.Items.Add(mo["Name"].ToString());
                }
            }
    

    Click here to download source and application demo

    Demo of application which listed network and local printer

    enter image description here

    0 讨论(0)
  • 2020-11-29 18:52

    Look at the static System.Drawing.Printing.PrinterSettings.InstalledPrinters property.

    It is a list of the names of all installed printers on the system.

    0 讨论(0)
  • 2020-11-29 18:56

    If you are working with MVC C#, this is the way to deal with printers and serial ports for dropdowns.

    using System.Collections.Generic;
    using System.Linq;
    using System.IO.Ports;
    using System.Drawing.Printing;
    
        public class Miclass
        {
            private void AllViews()
            {
                List<PortClass> ports = new List<PortClass>();
                List<Printersclass> Printersfor = new List<Printersclass>();
                string[] portnames = SerialPort.GetPortNames();
                /*PORTS*/
                for (int i = 0; i < portnames.Count(); i++)
                {
                    ports.Add(new PortClass() { Name = portnames[i].Trim(), Desc = portnames[i].Trim() });
                }
                /*PRINTER*/
                for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
                {
                    Printersfor.Add(new Printersclass() { Name = PrinterSettings.InstalledPrinters[i].Trim(), Desc = PrinterSettings.InstalledPrinters[i].Trim() });
                }
            }
        }
        public class PortClass
        {
            public string Name { get; set; }
            public string Desc { get; set; }
    
            public override string ToString()
            {
                return string.Format("{0} ({1})", Name, Desc);
            }
        }
        public class Printersclass
        {
            public string Name { get; set; }
            public string Desc { get; set; }
    
            public override string ToString()
            {
                return string.Format("{0} ({1})", Name, Desc);
            }
        }
    
    0 讨论(0)
提交回复
热议问题