C#获取本地打印机列表,并将指定打印机设置为默认打印机
2013-09-06 21:07
399 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Printers { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); InitprinterComboBox(); //初始化打印机下拉列表选项 } private void InitprinterComboBox() { List<String> list = LocalPrinter.GetLocalPrinters(); //获得系统中的打印机列表 foreach (String s in list) { printerComboBox.Items.Add(s); //将打印机名称添加到下拉框中 } } private void printButton_Click(object sender, RoutedEventArgs e) { if (printerComboBox.SelectedItem != null) //判断是否有选中值 { if (Externs.SetDefaultPrinter(printerComboBox.SelectedItem.ToString())) //设置默认打印机 { MessageBox.Show(printerComboBox.SelectedItem.ToString() + "设置为默认打印机成功!"); } else { MessageBox.Show(printerComboBox.SelectedItem.ToString() + "设置为默认打印机失败!"); } } } } }
LocalPrinter.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing.Printing; using System.Runtime.InteropServices; namespace Printers { class LocalPrinter { private static PrintDocument fPrintDocument = new PrintDocument(); //获取本机默认打印机名称 public static String DefaultPrinter() { return fPrintDocument.PrinterSettings.PrinterName; } public static List<String> GetLocalPrinters() { List<String> fPrinters = new List<String>(); fPrinters.Add(DefaultPrinter()); //默认打印机始终出现在列表的第一项 foreach (String fPrinterName in PrinterSettings.InstalledPrinters) { if (!fPrinters.Contains(fPrinterName)) { fPrinters.Add(fPrinterName); } } return fPrinters; } } }
Externs.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace Printers { class Externs { [DllImport("winspool.drv")] public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机 } }
相关文章推荐
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
- C#如何获取本地的打印机列表并且指定默认打印机
- 获取设置本地当前默认打印机
- C# 获取或设置本地打印机及配置文件操作
- C#获取和设置系统的默认打印机,使用API
- C#获取系统默认打印机和已安装的打印机列表
- VB6 获取和设置默认打印机
- C#获取指定数据库服务器下某用户可访问的数据库列表
- [.Net码农]c#中设置默认打印机
- C#:调用API函数,弹出对话框更改打印机默认设置
- c#(winform)获取本地打印机
- C#获取指定数据库服务器下某用户可访问的数据库列表
- C# 获取或设置指定 config 文件的值
- C#如何设置电脑的默认打印机?
- C# 获取打印机列表以及串口
- C# 获取本地安装的软件列表
- C#:调用API函数,弹出对话框更改打印机默认设置