码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-10-12 12:25:51      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

 

技术分享
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

 

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

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#获取本地打印机列表,并将指定打印机设置为默认打印机

标签:

原文地址:http://www.cnblogs.com/1175429393wljblog/p/4871143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!