C#枚举系统安装的所有打印机
生活随笔
收集整理的這篇文章主要介紹了
C#枚举系统安装的所有打印机
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在下面的程序中我們將把系統(tǒng)中所安裝的打印機(jī)用列表框列出來,同時(shí)為默認(rèn)打印機(jī)設(shè)置缺省值。
在下面的程序中我們用到了兩個(gè)主要的類,把所有的打印機(jī)列表出來用到了PrinterSettings 類,獲取系統(tǒng)默認(rèn)打印機(jī)用到了PrintDocument 類,下面我們就動(dòng)手實(shí)踐一下吧。
先新建一個(gè)windows form的工程,然后加入一個(gè)lable和一個(gè)comBox,就行啦,關(guān)鍵在下面啦,我們?nèi)绾潍@得默認(rèn)打印機(jī),就得用下面的語句。
PrintDocument prtdoc = new PrintDocument();
string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//獲取默認(rèn)的打印機(jī)名
有了默認(rèn)的打印機(jī),我們?cè)侔阉械拇蛴C(jī)列出來。
PrinterSettings類有一個(gè)InstalledPrinters的屬性,不知是做什么的吧,查MSDN如下解釋:
PrinterSettings.InstalledPrinters 獲取安裝在計(jì)算機(jī)上所有打印機(jī)的名稱。
在C#中如下定義:
[C#]
[Serializable]
[ComVisible(false)]
public static PrinterSettings.StringCollection InstalledPrinters
{get;}
屬性值
PrinterSettings.StringCollection,它表示安裝在計(jì)算機(jī)上所有打印機(jī)的名稱。
異常
異常類型 條件
Win32Exception 未能枚舉可用的打印機(jī)
備注
可以使用已安裝的打印機(jī)名稱的集合向用戶提供要打印到的打印機(jī)選擇。
下面的示例用已安裝的打印機(jī)填充 comboInstalledPrinters 組合框,并且還在選擇更改時(shí)使用 PrinterName 屬性設(shè)置用于打印的打印機(jī)。PopulateInstalledPrintersCombo 例程在窗體初始化時(shí)被調(diào)用。該示例假定存在名為 printDoc 的 PrintDocument 變量,并且存在特定的組合框。
[C#]
//下面括號(hào)內(nèi)的自己翻譯添加進(jìn)去的
private void PopulateInstalledPrintersCombo()
{
// Add list of installed printers found to the combo box.(將系統(tǒng)中所有的打機(jī)加入列表框)
// The pkInstalledPrinters string will be used to provide the display string.(列表框中顯示的字串由pkInstalledPrinters提供)
foreach(String pkInstalledPrinters in
PrinterSettings.InstalledPrinters)
{
comboInstalledPrinters.Items.Add(pkInstalledPrinters);
}
}
private void comboInstalledPrinters_SelectionChanged(object sender, System.EventArgs e)
{
// Set the printer to a printer in the combo box when the selection changes.(當(dāng)列表框改變時(shí)設(shè)置選擇的打印機(jī))
if (comboInstalledPrinters.SelectedIndex != -1)
{
// The combo box's Text property returns the selected item's text, which is the printer name.(將選擇的打印機(jī)名在列表框中顯示)
printDoc.PrinterSettings.PrinterName= comboInstalledPrinters.Text;
}
}
看了MSDN的說明,懂多了吧,下面是我寫練習(xí)完整代碼.
//程序說明:將系統(tǒng)中的所有打印機(jī)在列表框中列出
//程序變量: PrintDocument prtdoc、string strDefaultPrinter
//編寫人:蠶蛹(sillnet@163.net)
//日期:2003-03-20
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace PrinterList
{
///
/// Form1 的摘要說明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox printerList;
///
/// 必需的設(shè)計(jì)器變量。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
PrintDocument prtdoc = new PrintDocument();
string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//獲取默認(rèn)的打印機(jī)名
foreach(String strPrinter in PrinterSettings.InstalledPrinters)
//在列表框中列出所有的打印機(jī),
{
printerList.Items.Add(strPrinter);
if (strPrinter == strDefaultPrinter)//把默認(rèn)打印機(jī)設(shè)為缺省值
{
printerList.SelectedIndex = printerList.Items.IndexOf(strPrinter);
}
}
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
///
/// 清理所有正在使用的資源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.printerList = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 0;
this.label1.Text = "選擇打印機(jī):";
//
// printerList
//
this.printerList.Location = new System.Drawing.Point(88, 22);
this.printerList.Name = "printerList";
this.printerList.Size = new System.Drawing.Size(192, 21);
this.printerList.TabIndex = 1;
this.printerList.Text = "當(dāng)前系統(tǒng)未裝打印機(jī)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(288, 61);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.printerList,
this.label1});
this.Name = "Form1";
this.Text = "打印機(jī)列表";
this.ResumeLayout(false);
}
#endregion
///
/// 應(yīng)用程序的主入口點(diǎn)。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}?
?
在下面的程序中我們用到了兩個(gè)主要的類,把所有的打印機(jī)列表出來用到了PrinterSettings 類,獲取系統(tǒng)默認(rèn)打印機(jī)用到了PrintDocument 類,下面我們就動(dòng)手實(shí)踐一下吧。
先新建一個(gè)windows form的工程,然后加入一個(gè)lable和一個(gè)comBox,就行啦,關(guān)鍵在下面啦,我們?nèi)绾潍@得默認(rèn)打印機(jī),就得用下面的語句。
PrintDocument prtdoc = new PrintDocument();
string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//獲取默認(rèn)的打印機(jī)名
有了默認(rèn)的打印機(jī),我們?cè)侔阉械拇蛴C(jī)列出來。
PrinterSettings類有一個(gè)InstalledPrinters的屬性,不知是做什么的吧,查MSDN如下解釋:
PrinterSettings.InstalledPrinters 獲取安裝在計(jì)算機(jī)上所有打印機(jī)的名稱。
在C#中如下定義:
[C#]
[Serializable]
[ComVisible(false)]
public static PrinterSettings.StringCollection InstalledPrinters
{get;}
屬性值
PrinterSettings.StringCollection,它表示安裝在計(jì)算機(jī)上所有打印機(jī)的名稱。
異常
異常類型 條件
Win32Exception 未能枚舉可用的打印機(jī)
備注
可以使用已安裝的打印機(jī)名稱的集合向用戶提供要打印到的打印機(jī)選擇。
下面的示例用已安裝的打印機(jī)填充 comboInstalledPrinters 組合框,并且還在選擇更改時(shí)使用 PrinterName 屬性設(shè)置用于打印的打印機(jī)。PopulateInstalledPrintersCombo 例程在窗體初始化時(shí)被調(diào)用。該示例假定存在名為 printDoc 的 PrintDocument 變量,并且存在特定的組合框。
[C#]
//下面括號(hào)內(nèi)的自己翻譯添加進(jìn)去的
private void PopulateInstalledPrintersCombo()
{
// Add list of installed printers found to the combo box.(將系統(tǒng)中所有的打機(jī)加入列表框)
// The pkInstalledPrinters string will be used to provide the display string.(列表框中顯示的字串由pkInstalledPrinters提供)
foreach(String pkInstalledPrinters in
PrinterSettings.InstalledPrinters)
{
comboInstalledPrinters.Items.Add(pkInstalledPrinters);
}
}
private void comboInstalledPrinters_SelectionChanged(object sender, System.EventArgs e)
{
// Set the printer to a printer in the combo box when the selection changes.(當(dāng)列表框改變時(shí)設(shè)置選擇的打印機(jī))
if (comboInstalledPrinters.SelectedIndex != -1)
{
// The combo box's Text property returns the selected item's text, which is the printer name.(將選擇的打印機(jī)名在列表框中顯示)
printDoc.PrinterSettings.PrinterName= comboInstalledPrinters.Text;
}
}
看了MSDN的說明,懂多了吧,下面是我寫練習(xí)完整代碼.
//程序說明:將系統(tǒng)中的所有打印機(jī)在列表框中列出
//程序變量: PrintDocument prtdoc、string strDefaultPrinter
//編寫人:蠶蛹(sillnet@163.net)
//日期:2003-03-20
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace PrinterList
{
///
/// Form1 的摘要說明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox printerList;
///
/// 必需的設(shè)計(jì)器變量。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
PrintDocument prtdoc = new PrintDocument();
string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//獲取默認(rèn)的打印機(jī)名
foreach(String strPrinter in PrinterSettings.InstalledPrinters)
//在列表框中列出所有的打印機(jī),
{
printerList.Items.Add(strPrinter);
if (strPrinter == strDefaultPrinter)//把默認(rèn)打印機(jī)設(shè)為缺省值
{
printerList.SelectedIndex = printerList.Items.IndexOf(strPrinter);
}
}
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
///
/// 清理所有正在使用的資源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.printerList = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 0;
this.label1.Text = "選擇打印機(jī):";
//
// printerList
//
this.printerList.Location = new System.Drawing.Point(88, 22);
this.printerList.Name = "printerList";
this.printerList.Size = new System.Drawing.Size(192, 21);
this.printerList.TabIndex = 1;
this.printerList.Text = "當(dāng)前系統(tǒng)未裝打印機(jī)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(288, 61);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.printerList,
this.label1});
this.Name = "Form1";
this.Text = "打印機(jī)列表";
this.ResumeLayout(false);
}
#endregion
///
/// 應(yīng)用程序的主入口點(diǎn)。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}?
?
總結(jié)
以上是生活随笔為你收集整理的C#枚举系统安装的所有打印机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求你的承诺歌词
- 下一篇: C#中对POP3邮件解码