How to get the list of all printers in computer

前端 未结 6 1085
长发绾君心
长发绾君心 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: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 ports = new List();
                List Printersfor = new List();
                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);
            }
        }
    

提交回复
热议问题