Example #1
0
        public static List <PrinterInfo> GetSpoolDevicesInfo()
        {
            var printers         = new ManagementObjectSearcher("SELECT * from Win32_Printer");
            var spoolDevicesList = new List <PrintUtility.PrinterInfo>();

            foreach (var item in printers.Get())
            {
                var deviceCaption = item["Caption"];

                if (deviceCaption == null)
                {
                    continue;
                }

                var spoolDevice = new PrintUtility.PrinterInfo
                {
                    Name    = item["Name"].ToString(),
                    Network = null,
                    ComPort = null
                };

                spoolDevicesList.Add(spoolDevice);
            }

            return(spoolDevicesList);
        }
Example #2
0
        public static List <PrinterInfo> GetComDevicesInfo()
        {
            var options         = ProcessConnection.ProcessConnectionOptions();
            var connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2");

            var objectQuery        = new ObjectQuery("SELECT Name, Caption FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
            var comPortSearcher    = new ManagementObjectSearcher(connectionScope, objectQuery);
            var comPortDevicesList = new List <PrinterInfo>();

            using (comPortSearcher)
            {
                foreach (var item in comPortSearcher.Get())
                {
                    var deviceCaption = item["Caption"];

                    if (deviceCaption == null)
                    {
                        continue;
                    }

                    if (!deviceCaption.ToString().Contains("(COM"))
                    {
                        continue;
                    }

                    var comDevice = new PrintUtility.PrinterInfo
                    {
                        Name    = item["Name"].ToString(),
                        ComPort = deviceCaption.ToString().Substring(deviceCaption.ToString().LastIndexOf("(COM", StringComparison.Ordinal)).Replace("(", string.Empty).Replace(")", string.Empty)
                    };

                    comPortDevicesList.Add(comDevice);
                }
            }

            return(comPortDevicesList);
        }