Modifying trays in printer driver is not reflected in PaperSources

27 Views Asked by At

I have a running application to work with printers and their trays. My current implementation does not recognize added or removed trays of a printer via windows driver. To get these changes I need to restart the whole application. But if a printer gets removed or added the application is able to detect it.

Here is a short example which can be used to reproduce this behavior. Just let this run and meanwhile add or remove some trays from a printer and press enter to continue. It will now print the printers with the same trays again. If you would now remove or add a printer and press enter it will display the changes inside the console.

using System.Drawing.Printing;

namespace PrinterService;

internal class Program
{
    public static void Main(string[] args)
    {
        while (true)
        {
            var printerNames = PrinterSettings.InstalledPrinters.Cast<string>().ToList();

            foreach (var printerName in printerNames)
            {
                var printerSettings = new PrinterSettings { PrinterName = printerName };
                var trays = printerSettings.PaperSources
                    .Cast<PaperSource>()
                    .Where(ps => !string.IsNullOrWhiteSpace(ps.SourceName))
                    .Select(ps => ps.SourceName)
                    .ToArray();

                if (trays.Length == 0)
                    continue;

                Console.WriteLine($"{printerName} - {string.Join(", ", trays)}");
            }

            Console.WriteLine("-----------------------------------------------------------------");
            Console.ReadLine();
        }
    }
}

Do I miss something or is my use case simply not supported?

0

There are 0 best solutions below