public static List <PrinterPort> GetAllPorts() { PORT_INFO_2[] ports = GetAvailablePorts(""); List <PrinterPort> portList = new List <PrinterPort>(); if (ports == null) { return(portList); // retorna a lista vazia } foreach (PORT_INFO_2 port in ports) { PrinterPort printerPort = new PrinterPort(); printerPort.Name = port.pPortName; printerPort.Description = port.pDescription; printerPort.Type = port.fPortType; portList.Add(printerPort); } return(portList); }
public static PrinterPort GetPort(String portName) { PORT_INFO_2[] ports = GetAvailablePorts(""); if (ports == null) { return(null); // nenhuma porta encontrada } foreach (PORT_INFO_2 port in ports) { if (port.pPortName.ToUpper() == portName.ToUpper()) { PrinterPort printerPort = new PrinterPort(); printerPort.Name = port.pPortName; printerPort.Description = port.pDescription; printerPort.Type = port.fPortType; return(printerPort); } } return(null); }
/// <summary> /// Verifica se a impressora está ligada em rede (através de porta TCP/IP) /// Localiza a porta da impressora no dicionário de portas e checa a propriedade "netAttached" /// </summary> private static Boolean IsNetworkAttached(ManagementObject printer, Dictionary <String, PrinterPort> portDic) { Boolean isNetworkAttached = false; // Verifica se a porta da impressora está presente no dicionário if (!portDic.ContainsKey((String)printer["PortName"])) { return(false); } PrinterPort printerPort = portDic[(String)printer["PortName"]]; if (printerPort != null) { if ((printerPort.Type & PortTypeEnum.netAttached) == PortTypeEnum.netAttached) { isNetworkAttached = true; } } return(isNetworkAttached); }