C#获取本地打印机列表,并将指定打印机设置为默认打印机
来源:互联网 发布:桥接网络 编辑:程序博客网 时间:2024/05/21 11:29
C#获取本地打印机列表,并将指定打印机设置为默认打印机
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将指定名称的打印机设置为默认打印机 }}
0 0
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
- C#如何获取本地的打印机列表并且指定默认打印机
- 获取所有打印机,设置默认打印机,获取默认打印机
- C# 获取打印机列表
- C#获取打印机列表
- C# 指定默认的打印机
- 获取和设置默认打印机
- java 获取可用的打印机列表 和 当前默认打印机
- 获取默认打印机名称
- 设置默认打印机
- 设置默认打印机
- 代码设置默认打印机
- 设置默认打印机
- 设置系统默认打印机
- Windows设置默认打印机
- ABAP设置默认打印机
- 获取默认打印机纸张的设置
- iOS 之KVO简介
- spring mvc简单的demo
- iOS 之KVC简介
- Android系统性能调优工具介绍
- Java五道输出易错题解析
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
- 反渗透设备:RO反渗透设备应用范围详细解析
- 17周项目5--玩日期时间
- Python提取xml中的字段
- ACM函数模板开源
- oracle 分析函数完成号码区段的统计(连续的,不连续的加一条记录)
- 从事Android开发我们要注意哪些?
- 如何写出高效率的正则表达式
- 异常机制及throw与throws的区别