Search code examples
c#printingwindows-10printdocument

PrintDocument.PrinterSettings.Print() Changing default printer


I have the following code, to test out printing to a specific printer. The code sends the print to the correct printer. But I have notices upon completing the print, the systems default printer has changed.

I thought maybe at first maybe the PrinterName property was setting the default printer, and quickly realized that is not the case. I have to assume it is happening inside the Print() method.

I did some reading on changing the default printer, the solutions I found seemed to use the System.Management Namespace. But did not find anything related to changing the default printer back in the System.Drawing.Printing Namespace.

I figure there might be a simple way to change it back using the same Namespace that used it in the first place. Other than reprinting the document or a blank document to the previously default printer.

    static void Main(string[] args)
    {
        Receipt();
    }
    static private void Receipt()
    {
        PrintDocument p = new PrintDocument();
        p.PrinterSettings.PrinterName = "Star HSP7000 Receipt";
        p.PrintPage += delegate(object sender1, PrintPageEventArgs e)
        {
            e.Graphics.DrawString("testtesttestest", new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
        };
        p.Print();
    }

Solution

  • After asking this question, I continued searching, and found that windows 10 quietly manages the Default Printer to the last printer that is printed from (by default).

    If you are having an issue on a windows 10 machine where the default printer is changing after a print job. Make sure to change this. enter image description here