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///</sum Mary> public partial class Window1:window {public Window1 () {InitializeComponent (); Initprintercombobox (); Initialize printer drop-down list option} private void Initprintercombobox () {list<string> list = Localprint Er. Getlocalprinters (); Get the list of printers in the system foreach (String s in list) {PRINTERCOMBOBOX.ITEMS.ADD (s);//Add the printer name to the next Pull box}} private void printButton_Click (object sender, RoutedEventArgs e) {if ( Printercombobox.SelectedItem! = NULL)//Determine if there is a check value {if (Externs.setdefaultprinter (printerComboBox.SelectedItem.To String ()))//Set default printer {MessageBox.Show (printerComboBox.SelectedItem.ToString () + "set to default hit Printing machine Success! "); } else {MessageBox.Show (printerComboBox.SelectedItem.ToString () + "is set as the default Printer failed! "); } } } }}
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 (); Get the native default printer name public static String Defaultprinter () { return fPrintDocument.PrinterSettings.PrinterName; } public static list<string> Getlocalprinters () { list<string> fprinters = new List<string> ( ); Fprinters.add (Defaultprinter ()); The default printer always appears in the list of the first item, 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); Call the win API to set the specified name of the printer as the default printer} }
C # Gets a list of local printers and sets the specified printer as the default printer