[點晴永久免費OA]C#獲取本地打印機列表,并將指定打印機設置為默認打印機
當前位置:點晴教程→點晴OA辦公管理信息系統
→『 經驗分享&問題答疑 』
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() + "設置為默認打印機失敗!"); } } } } } 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; } } } 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將指定名稱的打印機設置為默認打印機 } } 該文章在 2022/7/2 14:15:22 編輯過 |
關鍵字查詢
相關文章
正在查詢... |