项目中,需要选择打印机,切换打印机。demo如下(wpf应用程序):

Xaml:

<Window x:Class="PrintersApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<ComboBox x:Name="printerComboBox"/>
<Button Content="设置成打印机" Click="setDefaultPrintBtn"/>
</StackPanel>
</Grid>
</Window>

打印机LocalPrinter类:

 public 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;
}
}

win api :

  public class Externs
{
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机
}

逻辑代码:

   /// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitprinterComboBox(); //初始化打印机下拉列表选项
} private void InitprinterComboBox()
{
List<String> list = LocalPrinter.GetLocalPrinters(); //获得系统中的打印机列表
foreach (String s in list)
{
printerComboBox.Items.Add(s); //将打印机名称添加到下拉框中
}
} private void setDefaultPrintBtn(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() + "设置为默认打印机失败!");
}
} }
}

运行截图:

下拉ComboBox,选择要设置为默认的打印机,点击“设置成默认打印机”按钮,设置成功:

C#设置默认打印机的更多相关文章

  1. windows设置默认打印机

    实现这个功能需要使用windows api [DllImport("winspool.drv")] public static extern bool SetDefaultPrin ...

  2. Winform 判断打印机是否可用,实现设置默认打印机功能

    Winform 判断打印机是否可用,实现设置默认打印机功能 http://www.cnblogs.com/zfanlong1314/p/3878563.html

  3. C#Winfrom系统打印机调用/设置默认打印机

    实现如下效果: 实现方式如下: using System;using System.Drawing.Printing;using System.Runtime.InteropServices;usin ...

  4. VB6 获取和设置默认打印机

    Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA&quo ...

  5. C#获取本地打印机列表,并将指定打印机设置为默认打印机

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  6. 利用ActiveX实现web页面设置本地默认打印机、纸张大小

    通常web技术无法设置本地计算机的默认打印机,包括用代码设置纸张大小,如果业务系统中真遇到这种需求,只能通过其它辅助手段(比如ActiveX)实现.下面这段代码,出自网上被广泛使用的"泥人张 ...

  7. PC设置局域网打印机

    打印机采用局域网网络连接方式,下面以Windows系统为例说明如何添加此打印机. 将电脑接入局域网 在“控制面板”中打开“设备与打印机”,点击“添加打印机” 在弹出列表中,会自动出现打印机型号,选中它 ...

  8. cad.net cad启动慢? cad2008启动慢? cad启动延迟? cad卡住? cad98%卡? 默认打印机!!

    默认打印机是不是联网打印机,如果cad找不到这个打印机将会很慢才打开cad的界面(它真的不是卡死了,而是找不到...) 奇妙的是桌子至今都没有利用新建线程的方式来控制这个打印机等待,而是直接在主程序上 ...

  9. 如何以编程方式打印到在 MFC 中的非默认打印机

    http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105790245b09c0252bd7a74a2485d315d2390f0750 ...

随机推荐

  1. 2016年团体程序设计天梯赛-决赛 L1-5. 是不是太胖了(5)

    据说一个人的标准体重应该是其身高(单位:厘米)减去100.再乘以0.9所得到的公斤数.已知市斤是公斤的两倍.现给定某人身高,请你计算其标准体重应该是多少?(顺便也悄悄给自己算一下吧……) 输入格式: ...

  2. Eclipse Kepler maven工程配置JDK1.8

    首先需要下载插件:"Help" --> "Market Place" --> Search for java 8 kepler. install J ...

  3. Node.js:服务器与数据流

    1.Node 常被用来构建服务器,下面代码就是创建了一个服务器. var http = require('http'); var server = http.createServer(); serve ...

  4. git 和 svn

    GIT和SVN之间的五个基本区别 如果你在读这篇文章,说明你跟大多数开发者一样对GIT感兴趣,如果你还没有机会来试一试GIT,我想现在你就要了解它了. GIT不仅仅是个版本控制系统,它也是个内容管理系 ...

  5. txt文件的读取

    两个函数:textread或importdata [textread函数] 格式:I=textread('文件名.txt','列',读取的行数,'headerlines',跳过的头行数);  返回值I ...

  6. infix to postfix 完整版

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  7. Struts2配置文件动态传参

    两个Action动态传递参数: 1.从一个Action传递到另一个Action如果带有参数那么result中的type属性需要使用redirect 第一个Action代码如下: private Str ...

  8. ebtables使用的协议

    ebtables的参数中: -p:指明使用的协议类型,ipv4,arp等可选(使用时必选)详情见/etc/ethertypes --ip-proto:IP包的类型,1为ICMP包,6为TCP包,17为 ...

  9. gcc-config: Active gcc profile is invalid解决办法

    错误描述 Gentoo软件安装错误,提示: gcc-config: Active gcc profile is invalid 解决方法: 列出可用的profile gcc-config -l gcc ...

  10. Python 之 geturl 学习

    geturl为response对象的方法,由于有时候得到的网站url并不是真正的初始url而是通过重定向获得的,所以可以通过geturl方法获取真实的url.测试代码如下: from urllib2 ...