1
using System;
2 using System.Collections.Generic;
3 using System.Windows.Forms;
4 using System.Drawing.Printing;
5 namespace FindPrinterDemo
6 {
7 public partial class Demo : Form
8 {
9 public Demo()
10 {
11 this.Text= " 本地打印机列表 ";
12 ListBox fListBox = new ListBox();
13 fListBox.Dock = DockStyle.Fill;
14 foreach (String fPrinterName in LocalPrinter.GetLocalPrinters())
15 fListBox.Items.Add(fPrinterName);
16 this.Controls.Add(fListBox);
17 }
18 }
19
20 /// </summary>
21 public class LocalPrinter
22 {
23 private static PrintDocument fPrintDocument = new PrintDocument();
24 /// <summary>
25 /// 获取本机默认打印机名称
26 /// </summary>
27 public static String DefaultPrinter
28 {
29 get { return fPrintDocument.PrinterSettings.PrinterName; }
30 }
31 /// <summary>
32 /// 获取本机的打印机列表。列表中的第一项就是默认打印机。
33 /// </summary>
34 public static List<String> GetLocalPrinters()
35 {
36 List<String> fPrinters = new List< string>();
37 fPrinters.Add(DefaultPrinter); // 默认打印机始终出现在列表的第一项
38 foreach (String fPrinterName in PrinterSettings.InstalledPrinters)
39 {
40 if (!fPrinters.Contains(fPrinterName))
41 fPrinters.Add(fPrinterName);
42 }
43 return fPrinters;
44 }
45 }
46 }
2 using System.Collections.Generic;
3 using System.Windows.Forms;
4 using System.Drawing.Printing;
5 namespace FindPrinterDemo
6 {
7 public partial class Demo : Form
8 {
9 public Demo()
10 {
11 this.Text= " 本地打印机列表 ";
12 ListBox fListBox = new ListBox();
13 fListBox.Dock = DockStyle.Fill;
14 foreach (String fPrinterName in LocalPrinter.GetLocalPrinters())
15 fListBox.Items.Add(fPrinterName);
16 this.Controls.Add(fListBox);
17 }
18 }
19
20 /// </summary>
21 public class LocalPrinter
22 {
23 private static PrintDocument fPrintDocument = new PrintDocument();
24 /// <summary>
25 /// 获取本机默认打印机名称
26 /// </summary>
27 public static String DefaultPrinter
28 {
29 get { return fPrintDocument.PrinterSettings.PrinterName; }
30 }
31 /// <summary>
32 /// 获取本机的打印机列表。列表中的第一项就是默认打印机。
33 /// </summary>
34 public static List<String> GetLocalPrinters()
35 {
36 List<String> fPrinters = new List< string>();
37 fPrinters.Add(DefaultPrinter); // 默认打印机始终出现在列表的第一项
38 foreach (String fPrinterName in PrinterSettings.InstalledPrinters)
39 {
40 if (!fPrinters.Contains(fPrinterName))
41 fPrinters.Add(fPrinterName);
42 }
43 return fPrinters;
44 }
45 }
46 }