程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> [C#] 獲取打印機列表,

[C#] 獲取打印機列表,

編輯:C#入門知識

[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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved