[Windows Forms C#] Changing default Printer muiltiple times

שלומי ספיר 96 Reputation points
2021-03-28T22:33:05.06+00:00

Hello everyone I hope this is the right place for this Im trying to change default Printer back and forth depanding on what I print through Code so I search the web and found this Code

 public static class MyPrinters
        {
            [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool SetDefaultPrinter(string Name);
        }

And this what I set before printing

MyPrinters.SetDefaultPrinter(PrinterWhitePage);
MyPrinters.SetDefaultPrinter(PrinterLogoPage);

The code of switching the Default Printer works i see in Control Panel the Green V is moving between the printer but as soon as I print 1 page even though the Default Printer is changing it always will print form the 1st chosen printer to fix this i need to exit to program and set the second printer is there any code line to force Refresh Default Printer without reopen the program?

Please Help

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
0 comments No comments
{count} votes

Accepted answer
  1. שלומי ספיר 96 Reputation points
    2021-03-29T17:34:57.117+00:00

    I solved the issue by Making a diffrent WebBrowser

    for some reason if you use a printer for a specific WebBrowser it will always print from that printer
    until you reopen the program.

    Making 2 WebBrowsers and assign a printer for each solved the issue.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-03-29T05:50:50.52+00:00

    Hi 11062125,
    First, you can get the list of all printers attached to your PC and fill the values into a list.
    Then you can choose one via its name as a default Printer.
    Please refer to the following code:

    private void listAllPrinters()  
            {  
                foreach (var item in PrinterSettings.InstalledPrinters)  
                {      
                    this.listBox1.Items.Add(item.ToString());  
                }  
            }  
      
            private void listBox1_SelectedValueChanged(object sender, EventArgs e)  
            {  
                string pname = this.listBox1.SelectedItem.ToString();  
                myPrinters.SetDefaultPrinter(pname);  
            }  
      
      
            public Form1()  
            {  
                InitializeComponent();  
                listAllPrinters();  
            }  
        }  
      
        public static class myPrinters  
        {  
            [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]  
            public static extern bool SetDefaultPrinter(string Name);  
      
        }  
    

    You can also explore it from the PrintDialog point of view.
    Here is a related thread and hope helpful for you.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Castorix31 81,061 Reputation points
    2021-03-29T06:55:52.607+00:00

    You can just change PrinterSettings.PrinterName if you want to print to different printers

    A test (add a button for the click...) =>

        private Font printFont;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            PrintDocument printDocument1 = new PrintDocument();
            printFont = new Font("Arial", 16);
            printDocument1.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            printDocument1.PrinterSettings.PrinterName = "Microsoft Print to PDF";
            printDocument1.Print();
            printDocument1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
            printDocument1.Print();            
        }
    
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            float yPos = 0;
            ev.Graphics.DrawString("This is a test", printFont, Brushes.Red, leftMargin, yPos, new StringFormat());
        }
    
    0 comments No comments