System.Drawing.Printing.PrinterSettings.PrinterName gives wrong printer

Sevanthalingam, Vivek 11 Reputation points
2021-11-25T13:14:08.34+00:00

When System.Drawing.Printing.PrinterSettings.PrinterName is used for identifying the default printer, it gives the wrong value(Last installed printer in the system). The application is running as a Windows service with Log On user as Local system account and it is expected to print to the system's default printer .

When the same application is running as desktop application, it returns the correct default printer.

When the application is running as Windows service:
System.Drawing.Printing.PrinterSettings.PrinterName returns Printer A as default printer. PrinterSettings.IsDefaultPrinter is also "true".
In the system, Printer B is set as default printer.

When the same application is running as desktop application,
System.Drawing.Printing.PrinterSettings.PrinterName returns Printer B
In the system, Printer B is set as default printer.

My understanding is System.Drawing.Printing.PrinterSettings.PrinterName always gives the system default printer. If not PrinterSettings.IsDefaultPrinter can be used to identify the default printer but it looks like System.Drawing.Printing.PrinterSettings.PrinterName property is giving wrong value.

Because of this the service is always printing to the latest installed printer rather than printing to the default printer in the system. I verified there are no setup problem in the system. Is this a problem with System.Drawing dll

The printers are connected to the system directly.

Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C#
0 comments No comments
{count} vote

5 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2021-11-25T14:04:34.127+00:00

    The application is running as a Windows service

    There is in the doc at system.drawing.printing :

    Caution
    Classes within the System.Drawing.Printing namespace are not supported for use within a Windows service or ASP.NET application or service.

    2 people found this answer helpful.
    0 comments No comments

  2. S.Sengupta 24,716 Reputation points MVP
    2021-11-25T13:23:35+00:00
    0 comments No comments

  3. Bruce Barker 801 Reputation points
    2021-11-25T16:31:50.853+00:00

    Services don’t have a user profile, so there is no default printer selected.

    0 comments No comments

  4. Sevanthalingam, Vivek 11 Reputation points
    2021-11-30T12:52:34.327+00:00

    Any suggestions for getting default printer which will work in case of service also?


  5. Limitless Technology 39,931 Reputation points
    2021-12-14T17:47:15.677+00:00

    Hi there,

    IsDefaultPrinter always returns false when you explicitly set the PrinterName property to a string value other than null.Try this out

    GetDefaultPrinter()

    { PrinterSettings settings = new PrinterSettings();

    foreach (string printer in PrinterSettings.InstalledPrinters)

    { settings.PrinterName = printer;

    if (settings.IsDefaultPrinter)

    return printer;

    }

    return string.Empty;

    }


    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.