[C#] 获取打印机列表

一:获得本地安装的打印机列表

注:(如果在“设备和打印机”中已经添加了局域网的打印机设备,也算是本地安装的打印机;没有添加的则算作局域网打印机)

1,通过C#中PrinterSettings对象获取,如下,然后通过foreach即可遍历printers;

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

2,PrintHelper类以及方法GetEnumPrinters2,见文章最后部分;

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,通过ManagementObjectCollection(需要添加System.Management引用)来查询本地计算机中的打印机列表;

 1 // 获取本地打印机列表

 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

二:获取局域网打印机列表

1,PrintHelper类以及方法GetEnumPrinters2,见文章最后部分;

 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 pcbNeeded = 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 pcbNeeded, ref pcReturned);

16         if (pcbNeeded != 0)

17         {

18             pPrInfo4 = Marshal.AllocHGlobal((int)pcbNeeded);

19             size = pcbNeeded;

20             PrintHelper.EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_NAME, pNewName, 1, pPrInfo4, size, ref pcbNeeded, ref pcReturned);

21             PRINTER_INFO_1[] printerInfo1 = new PRINTER_INFO_1[pcReturned];

22             int offset = pPrInfo4.ToInt32();

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类以及相关枚举和信息类定义

 1 public class PrintHelper

 2 {

 3     [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]

 4     [return: MarshalAs(UnmanagedType.Bool)]

 5     public static extern bool EnumPrinters(PrinterEnumFlags Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuff, ref uint pcbNeeded, ref uint pcReturned);

 6 

 7     // 获取网络打印机

 8     public static PRINTER_INFO_1[] GetEnumPrinters(PrinterEnumFlags flags)

 9     {

10         PRINTER_INFO_1[] printerInfo1 = new PRINTER_INFO_1[] { };

11         uint pcbNeeded = 0;

12         uint pcReturned = 0;

13         IntPtr pPrInfo4 = IntPtr.Zero;

14         uint size = 0;

15         if (EnumPrinters(flags, null, 1, IntPtr.Zero, size, ref pcbNeeded, ref pcReturned))

16         {

17             return printerInfo1;

18         }

19         if (pcbNeeded != 0)

20         {

21             pPrInfo4 = Marshal.AllocHGlobal((int)pcbNeeded);

22             size = pcbNeeded;

23             EnumPrinters(flags, null, 1, pPrInfo4, size, ref pcbNeeded, ref pcReturned);

24             if (pcReturned != 0)

25             {

26                 printerInfo1 = new PRINTER_INFO_1[pcReturned];

27                 int offset = pPrInfo4.ToInt32();

28                 Type 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                 Marshal.FreeHGlobal(pPrInfo4);

36             }

37         }

38 

39         return printerInfo1;

40     }

41 

42     // 获取本地打印机

43     public static PRINTER_INFO_2[] GetEnumPrinters2()

44     {

45         PRINTER_INFO_2[] printerInfo2 =  new PRINTER_INFO_2[] { }; 

46         uint pcbNeeded = 0;

47         uint pcReturned = 0;

48         IntPtr pPrInfo4 = IntPtr.Zero;

49         if (EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref pcbNeeded, ref pcReturned))

50         {

51             return printerInfo2;

52         }

53         if (pcbNeeded != 0)

54         {

55             pPrInfo4 = Marshal.AllocHGlobal((int)pcbNeeded);

56             EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pPrInfo4, pcbNeeded, ref pcbNeeded, 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     PRINTER_ENUM_CONNECTIONS = 0x00000004,

 7     PRINTER_ENUM_FAVORITE = 0x00000004,

 8     PRINTER_ENUM_NAME = 0x00000008,

 9     PRINTER_ENUM_REMOTE = 0x00000010,

10     PRINTER_ENUM_SHARED = 0x00000020,

11     PRINTER_ENUM_NETWORK = 0x00000040,

12     PRINTER_ENUM_EXPAND = 0x00004000,

13     PRINTER_ENUM_CONTAINER = 0x00008000,

14     PRINTER_ENUM_ICONMASK = 0x00ff0000,

15     PRINTER_ENUM_ICON1 = 0x00010000,

16     PRINTER_ENUM_ICON2 = 0x00020000,

17     PRINTER_ENUM_ICON3 = 0x00040000,

18     PRINTER_ENUM_ICON4 = 0x00080000,

19     PRINTER_ENUM_ICON5 = 0x00100000,

20     PRINTER_ENUM_ICON6 = 0x00200000,

21     PRINTER_ENUM_ICON7 = 0x00400000,

22     PRINTER_ENUM_ICON8 = 0x00800000,

23     PRINTER_ENUM_HIDE = 0x01000000

24 }
PrinterEnumFlags
 1 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

 2 public struct PRINTER_INFO_1

 3 {

 4     int flags;

 5     [MarshalAs(UnmanagedType.LPTStr)]

 6     public string pDescription;

 7     [MarshalAs(UnmanagedType.LPTStr)]

 8     public string pName;

 9     [MarshalAs(UnmanagedType.LPTStr)]

10     public string pComment;

11 }
PRINTER_INFO_1
 1 [StructLayout(LayoutKind.Sequential)]

 2 public struct PRINTER_INFO_2

 3 {

 4     [MarshalAs(UnmanagedType.LPTStr)]

 5     public string pServerName;

 6     [MarshalAs(UnmanagedType.LPTStr)]

 7     public string pPrinterName;

 8     [MarshalAs(UnmanagedType.LPTStr)]

 9     public string pShareName;

10     [MarshalAs(UnmanagedType.LPTStr)]

11     public string pPortName;

12     [MarshalAs(UnmanagedType.LPTStr)]

13     public string pDriverName;

14     [MarshalAs(UnmanagedType.LPTStr)]

15     public string pComment;

16     [MarshalAs(UnmanagedType.LPTStr)]

17     public string pLocation;

18     public IntPtr pDevMode;

19     [MarshalAs(UnmanagedType.LPTStr)]

20     public string pSepFile;

21     [MarshalAs(UnmanagedType.LPTStr)]

22     public string pPrintProcessor;

23     [MarshalAs(UnmanagedType.LPTStr)]

24     public string pDatatype;

25     [MarshalAs(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

 

参考资料:

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

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

你可能感兴趣的:(C#)