question

11062125 avatar image
0 Votes"
11062125 asked 11062125 answered

[Windows Forms C#] Changing default Printer muiltiple times

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

11062125 avatar image
0 Votes"
11062125 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered 11062125 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is just another way how to activate the code
After the the first use of a printer it wont change even though the default printer is changing through the listbox

0 Votes 0 ·

This is just another way how to activate the code
After the the first use of a printer it wont change even though the default printer is changing through the listbox

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

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());
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.