#获取系统默认打印机和已安装的打印机列表?

  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.     /// <summary>  
  20.     /// 标题:获取本地打印机信息  
  21.     /// 
  22.     /// 日期:2009-09-22  
  23.     /// </summary>  
  24.     public class LocalPrinter  
  25.     {  
  26.         private static PrintDocument fPrintDocument = new PrintDocument();  
  27.         /// <summary>  
  28.         /// 获取本机默认打印机名称  
  29.         /// </summary>  
  30.         public static String DefaultPrinter  
  31.         {  
  32.             get { return fPrintDocument.PrinterSettings.PrinterName; }  
  33.         }  
  34.         /// <summary>  
  35.         /// 获取本机的打印机列表。列表中的第一项就是默认打印机。  
  36.         /// </summary>  
  37.         public static List<String> GetLocalPrinters()  
  38.         {  
  39.             List<String> fPrinters = new List<string>();  
  40.             fPrinters.Add(DefaultPrinter); // 默认打印机始终出现在列表的第一项  
  41.             foreach (String fPrinterName in PrinterSettings.InstalledPrinters)  
  42.             {  
  43.                 if (!fPrinters.Contains(fPrinterName))  
  44.                     fPrinters.Add(fPrinterName);  
  45.             }  
  46.             return fPrinters;  
  47.         }  
  48.     }  
  49. }  
posted @ 2012-12-04 20:25  Net-Spider  阅读(731)  评论(0)    收藏  举报