[C #] Getting the printer list,

Source: Internet
Author: User

[C #] Getting the printer list,
I. Obtain the list of locally installed printers

Note: (if you have added a LAN printer to the device and printer, it is also a locally installed printer. If you have not added a LAN printer, It is counted as a LAN printer)

1. Get the result through the PrinterSettings object in C #, as shown below, and then traverse the printers through foreach;

PrinterSettings. StringCollection printers = System. Drawing. Printing. PrinterSettings. InstalledPrinters;

2. For the PrintHelper class and the GetEnumPrinters2 method, see the last part of the article;

1 PRINTER_INFO_2 [] printers = PrintHelper. GetEnumPrinters2 (); 2 foreach (PRINTER_INFO_2 printer in printers) 3 {4 listBox1.Items. Add (printer. pPrinterName); 5}View Code

3. Use ManagementObjectCollection (System. Management reference must be added) to query the printer list on the local computer;

1 // get the local printer List 2 ManagementObjectCollection queryCollection; 3 string SQL = "SELECT * FROM Win32_Printer"; 4 Dictionary <string, ManagementObject> dict = new Dictionary <string, managementObject> (); 5 ManagementObjectSearcher query = new ManagementObjectSearcher (SQL); 6 queryCollection = query. get (); 7 foreach (ManagementObject mo in queryCollection) 8 {9 string oldName = mo ["Name"]. toString (); 10 listBox1.Items. add (oldName); 11}View Code 2: Obtain the LAN printer list

1. For the PrintHelper class and the GetEnumPrinters2 method, see the last part of the article;

1 PRINTER_INFO_1 [] printers = PrintHelper. GetEnumPrinters (); 2 foreach (PRINTER_INFO_1 printer in printers) 3 {4 if (-1 = printer. pName. IndexOf ("!! ") 5 {6 listBox1.Items. add (printer. pName); 7} 8 else 9 {10 uint restart needed = 0; 11 uint pcReturned = 0; 12 IntPtr pPrInfo4 = IntPtr. zero; 13 uint size = 0; 14 string pNewName = printer. pName; 15 PrintHelper. enumPrinters (PrinterEnumFlags. PRINTER_ENUM_NAME, pNewName, 1, IntPtr. zero, size, ref joined needed, ref pcReturned); 16 if (joined needed! = 0) 17 {18 pPrInfo4 = Marshal. allocHGlobal (int) returns needed); 19 size = bytes needed; 20 PrintHelper. enumPrinters (PrinterEnumFlags. PRINTER_ENUM_NAME, pNewName, 1, pPrInfo4, size, ref required needed, ref pcReturned); 21 PRINTER_INFO_1 [] printerInfo1 = new PRINTER_INFO_1 [pcReturned]; 22 int offset = ignore (); 23 Type type = typeof (PRINTER_INFO_1); 24 int increment = Marshal. sizeOf (type); 25 for (int I = 0; I <pcReturned; I ++) 26 {27 printerInfo1 [I] = (PRINTER_INFO_1) Marshal. ptrToStructure (new IntPtr (offset), type); 28 offset + = increment; 29 listBox1.Items. add (printerInfo1 [I]. pName); 30} 31 Marshal. freeHGlobal (pPrInfo4); 32} 33} 34}View Code


PrintHelper class and related enumeration and information class definitions

1 public class PrintHelper 2 {3 [DllImport ("winspool. drv ", CharSet = CharSet. auto, SetLastError = true)] 4 [return: financialas (UnmanagedType. bool)] 5 public static extern bool EnumPrinters (PrinterEnumFlags Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuff, ref uint cannot needed, ref uint pcReturned ); 6 7 // get the Network Printer 8 public static PRINTER_INFO_1 [] GetEnumPrinters (PrinterEnumFlags flags) 9 {10 PRINTER_INFO_1 [] printerInfo1 = new PRINTER_INFO_1 [] {}; 11 uint restart needed = 0; 12 uint pcReturned = 0; 13 IntPtr pPrInfo4 = IntPtr. zero; 14 uint size = 0; 15 if (EnumPrinters (flags, null, 1, IntPtr. zero, size, ref required needed, ref pcReturned) 16 {17 return printerInfo1; 18} 19 if (required needed! = 0) 20 {21 pPrInfo4 = Marshal. allocHGlobal (int) effecneeded); 22 size = effecneeded; 23 EnumPrinters (flags, null, 1, pPrInfo4, size, ref effecneeded, ref pcReturned); 24 if (pcReturned! = 0) 25 {26 printerInfo1 = new PRINTER_INFO_1 [pcReturned]; 27 int offset = pPrInfo4.ToInt32 (); 28 Type = typeof (PRINTER_INFO_1); 29 int increment = Marshal. sizeOf (type); 30 for (int I = 0; I <pcReturned; I ++) 31 {32 printerInfo1 [I] = (PRINTER_INFO_1) Marshal. ptrToStructure (new IntPtr (offset), type); 33 offset + = increment; 34} 35 destination Al. freeHGlobal (pPrInfo4); 36} 37} 38 39 return printerInfo1; 40} 41 4 2 // obtain the local printer 43 public static PRINTER_INFO_2 [] GetEnumPrinters2 () 44 {45 PRINTER_INFO_2 [] printerInfo2 = new PRINTER_INFO_2 [] {}; 46 uint required needed = 0; 47 uint pcReturned = 0; 48 IntPtr pPrInfo4 = IntPtr. zero; 49 if (EnumPrinters (PrinterEnumFlags. PRINTER_ENUM_LOCAL, null, 2, IntPtr. zero, 0, ref required needed, ref pcReturned) 50 {51 return printerInfo2; 52} 53 if (required needed! = 0) 54 {55 pPrInfo4 = Marshal. allocHGlobal (int) has needed); 56 EnumPrinters (PrinterEnumFlags. PRINTER_ENUM_LOCAL, null, 2, pPrInfo4, incluneeded, ref incluneeded, ref pcReturned); 57 if (pcReturned! = 0) 58 {59 printerInfo2 = new PRINTER_INFO_2 [pcReturned]; 60 int offset = pPrInfo4.ToInt32 (); 61 for (int I = 0; I <pcReturned; I ++) 62 {63 printerInfo2 [I] = (PRINTER_INFO_2) Marshal. ptrToStructure (new IntPtr (offset), typeof (PRINTER_INFO_2); 64 offset + = Marshal. sizeOf (typeof (PRINTER_INFO_2); 65} 66 Marshal. freeHGlobal (pPrInfo4); 67} 68} 69 70 return printerInfo2; 71} 72}PrintHelper 1 [FlagsAttribute] 2 public enum PrinterEnumFlags 3 {4 PRINTER_ENUM_DEFAULT = 0x00000001, 5 PRINTER_ENUM_LOCAL = 0x00000002, 6 rows = 0x00000004, 7 rows = 0x00000004, 8 PRINTER_ENUM_NAME = 0x00000008, 9 records = 0x00000010,10 records = 0x00000020,11 records = 0x00000040,12 records = 0x00004000,13 records = 0x00008000,14 records = 00ff0000, 15 records = 0x000133, 16 records = 0x00020000,17 records = 0x00040000,18 records = 0x00080000,19 records = 0x00100000,20 records = 0x00200000,21 records = 0x00400000,22 records = 0 x 0100000024, 23 PRINTER_ENUM_HIDE = 0 x}PrinterEnumFlags 1 [StructLayout (LayoutKind. sequential, CharSet = CharSet. auto)] 2 public struct PRINTER_INFO_1 3 {4 int flags; 5 [financialas (UnmanagedType. LPTStr)] 6 public string pDescription; 7 [financialas (UnmanagedType. LPTStr)] 8 public string pName; 9 [financialas (UnmanagedType. LPTStr)] 10 public string pComment; 11}PRINTER_INFO_1 1 [StructLayout (LayoutKind. sequential)] 2 public struct PRINTER_INFO_2 3 {4 [financialas (UnmanagedType. LPTStr)] 5 public string pServerName; 6 [financialas (UnmanagedType. LPTStr)] 7 public string pPrinterName; 8 [financialas (UnmanagedType. LPTStr)] 9 public string pShareName; 10 [financialas (UnmanagedType. LPTStr)] 11 public string pPortName; 12 [financialas (UnmanagedType. LPTStr)] 13 public string pDriverName; 14 [financialas (UnmanagedType. LPTStr)] 15 public string pComment; 16 [financialas (UnmanagedType. LPTStr)] 17 public string pLocation; 18 public IntPtr pDevMode; 19 [financialas (UnmanagedType. LPTStr)] 20 public string jsonpfile; 21 [exploralas (UnmanagedType. LPTStr)] 22 public string pPrintProcessor; 23 [financialas (UnmanagedType. LPTStr)] 24 public string pDatatype; 25 [financialas (UnmanagedType. LPTStr)] 26 public string pParameters; 27 public IntPtr pSecurityDescriptor; 28 public uint Attributes; 29 public uint Priority; 30 public uint DefaultPriority; 31 public uint StartTime; 32 public uint UntilTime; 33 public uint Status; 34 public uint cJobs; 35 public uint AveragePPM; 36}PRINTER_INFO_2

 

References:

Http://www.pinvoke.net/default.aspx/winspool.EnumPrinters

Https://msdn.microsoft.com/en-us/library/windows/desktop/dd162692 (v = vs.85). aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.