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将指定名称的打印机设置为默认打印机
}
}

C#获取本地打印机列表,并将指定打印机设置为默认打印机的更多相关文章
- 【VC++技术杂谈002】打印技术之获取及设置系统默认打印机
本文主要介绍如何获取以及设置系统的默认打印机. 1.获取系统中的所有打印机 获取系统中的所有打印机可以使用EnumPrinters()函数,该函数可以枚举全部的本地.网络打印机信息.其函数原型为: B ...
- LODOP指定window默认打印机和临时默认打印机
通过以下语句,可指定windows默认打印机LODOP.SET_PRINT_MODE("WINDOW_DEFPRINTER",某打印机名或序号);这种默认打印机是指的windows ...
- C#设置默认打印机
项目中,需要选择打印机,切换打印机.demo如下(wpf应用程序): Xaml: <Window x:Class="PrintersApp.MainWindow" xmlns ...
- [C#] 获取打印机列表
一:获得本地安装的打印机列表 注:(如果在"设备和打印机"中已经添加了局域网的打印机设备,也算是本地安装的打印机:没有添加的则算作局域网打印机) 1,通过 C# 中 Printer ...
- 获取WINDOWS打印机列表
获取WINDOWS打印机列表 如何知道WINDOWS已经安装了哪些打印机? 1) usesVcl.Printers 2) Printer.Printers // property Printers: ...
- C#Winfrom系统打印机调用/设置默认打印机
实现如下效果: 实现方式如下: using System;using System.Drawing.Printing;using System.Runtime.InteropServices;usin ...
- windows设置默认打印机
实现这个功能需要使用windows api [DllImport("winspool.drv")] public static extern bool SetDefaultPrin ...
- 如何以编程方式打印到在 MFC 中的非默认打印机
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105790245b09c0252bd7a74a2485d315d2390f0750 ...
- C# 获取打印机列表以及串口
C# 获取打印机列表以及默认打印机.串口列表. /// <summary> /// 获取本地已安装的打印机 /// </summary> /// <returns> ...
随机推荐
- Visual Studio 2015和.Net 2015 预览版在线安装和ISO镜像安装光盘下载
微软刚刚宣布了 Visual Studio 2015和.Net 2015 预览版,并同时提供了下载. 微软在纽约正进行中的#Connect# 全球开发者在线大会上宣布了Visual Studio 20 ...
- Review PHP设计模式之——单例模式
单例模式: class Single { private static $_instance; private function __construct(){ //define method as p ...
- Catalyst揭秘 Day1 Catalyst本地解析
Catalyst揭秘 Day1 Catalyst本地解析 今天开始讲下Catalyst,这是我们必须精通的内容之一: 在Spark2.x中,主要会以Dataframe和DataSet为api,无论是D ...
- vsftpd本地用户登录密码错误
今天发现自己虚拟机的vsftp使用本地用户名无法登陆,于是重新配置,但配置了很多次都没成功,一直显示 530 Login incorrect. Login failed 尝试了网上很多修改PAM的方案 ...
- Oracle中SAVEPOINT和ROLLBACK用法
savepoint是事务内部允许部分rollback的标志符.因为事务中对记录做了修改,我们可以在事务中创建savepoint来标识不同的点.如果遇到错误,就可以rollback到不同的点或直接回来事 ...
- PDF合并
要求:将多个table导出到一个PDF里,然后打印. 问题分析:要求将四个table放一个PDF打印,四个table的列各不相同,第一个是表头,其他三个是列表,列比表头多很多,如果直接生成一个exce ...
- java实现附件预览(openoffice+swfTools+FlexPaper) (转载)
下边例子是在网上找了一个网友做的例子,在次记录 1.概述 主要原理 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 2.通过swfTools将pd ...
- HDU 3487 Splay
给定两种操作,一种是把一个数列的某一段切下来插到剩余数列的某一个位置上. 一种是翻转操作,把数列的某一段进行翻转. 都是Splay的基本操作.标准的Rotateto调整出 [a,b]区间.然后对[a, ...
- jquery pass parameter to ajax callback
$('.del').on('click', function () { var id = $(this).attr('id'); var url = '/m/g2_content_del/' + id ...
- 前端跨域之html5 XMLHttpRequest Level2
前端代码 var xhr=new XMLHttpRequest(); xhr.open('POST','http://127.0.0.1:8081/ceshi',true); xhr.onreadys ...