C # Gets a list of local printers and sets the specified printer as the default printer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.