Exemplo n.º 1
0
        /// <summary>
        /// Enumerate all printer queues available to the local system.
        /// </summary>
        /// <returns>An array of PrinterDeviceInfo objects.</returns>
        /// <remarks></remarks>
        public static PrinterDeviceInfo[] EnumPrinters()
        {
            var p = _internalEnumerateDevices <PrinterDeviceInfo>(DevProp.GUID_DEVINTERFACE_PRINTER, ClassDevFlags.DeviceInterface | ClassDevFlags.Present);

            if (p is object && p.Count() > 0)
            {
                foreach (var x in p)
                {
                    if (string.IsNullOrEmpty(x.FriendlyName))
                    {
                        continue;
                    }
                    try
                    {
                        x.PrinterInfo = PrinterObject.GetPrinterInfoObject(x.FriendlyName);
                    }
                    catch
                    {
                        // can't connect to that printer!
                        continue;
                    }
                }
            }

            if (p is null)
            {
                return(null);
            }
            Array.Sort(p, new Comparison <PrinterDeviceInfo>((x, y) => { if (x.FriendlyName is object && y.FriendlyName is object)
                                                                         {
                                                                             return(string.Compare(x.FriendlyName, y.FriendlyName));
                                                                         }
                                                                         else
                                                                         {
                                                                             return(string.Compare(x.Description, y.Description));
                                                                         } }));
            return(p);
        }