Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Why can I only correctly detect the Default Printer name after a system reboot?

Writer Matthew Harrington

EDIT START 08/062023 - I will delete this question shortlyAfter deploying this onto a Windows 2019 server the issue disappears, it seems that this behaviour is only exhibited on Windows 11... very annoying.EDIT END

I'm trying to reliably detect the default printer that is set on a machine using asp.net c# while running the website in debug mode using Visual Studio 2022 on a Windows 11 pro machine.

I understand that Windows may cache printer information, including the default printer, to improve performance. In some cases, the cached information may not reflect the latest changes made to the default printer. Rebooting the system can clear the cache and force Windows to re-evaluate the printer configuration.... Is there a way to prevent this? Or perhaps to clear the cache so that a reboot isn't required?

Changing the default printer seems to work in the windows OS interface, i.e. it marks the appropriate printer with the 'default' tag.

I am trying the following two methods:

using System.Management;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
ManagementObjectCollection printerCollection = searcher.Get();
string defaultPrinterName = string.Empty;
foreach (ManagementObject printer in printerCollection)
{ bool isDefault = (bool)printer["Default"]; if (isDefault) { defaultPrinterName = printer["Name"].ToString(); }
}

and

PrinterSettings printerSettings = new PrinterSettings();
string defaultPrinterName = printerSettings.PrinterName;

The issue is that with both methods will work correctly after a system reboot, however if I change the default printer on a system and then run the above code it shows me only the default printer when the system restarted (not the currently selected default printer (marked as default in windows printers and devices screen)).

I've also noticed that if I install a new printer and set that as default then that will be detected as the default printer without requiring a system reboot.

On the off chance I've tried restarting the Print Spooler service and the Printer Extensions and Notifications service and also tried stopping and starting the Application Pool (In IIS), I've stopped Visual Studio 2022 runnign the website and rebuilt it and rerun it, but the only thing I can find that works is a system reboot!?!?!?

How can I detect the default printer without rebooting every time it is changed?

6

1 Answer

Ok, as a general rule, if you change the default, it only works for your current desktop session.

However, you can change the current computer default.

This seems to work:

 <div> <asp:ListBox runat="server" Width="344px" Height="551px"> </asp:ListBox> <br /> <br /> <asp:Button runat="server" Text="Set Default printer" OnClick="cmdSetPtr_Click" /> </div>

And code behind:

 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) LoadPrinters(); } void LoadPrinters() { PrinterSettings OnePrinter = new PrinterSettings(); foreach (string PrinterName in PrinterSettings.InstalledPrinters) { ListItem PtrItem = new ListItem(PrinterName); OnePrinter.PrinterName = PrinterName; if (OnePrinter.IsDefaultPrinter) PtrItem.Selected = true; ListBox1.Items.Add(PtrItem); } } protected void cmdSetPtr_Click(object sender, EventArgs e) { string PrinterName = ListBox1.SelectedItem.Text; SetDefaultPrinter(PrinterName); } [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool SetDefaultPrinter(string Name);

So then this:

enter image description here

In above, I change the printer, and then exit and re-start (f5) from VS, and the setting does persist.

And if I check in system (printers) - it also been changed.

Eg here:

enter image description here

And here is a routine to get default printer.

 public string GetDefaultPrinter() { PrinterSettings OnePrinter = new PrinterSettings(); foreach (string PrinterName in PrinterSettings.InstalledPrinters) { OnePrinter.PrinterName = PrinterName; if (OnePrinter.IsDefaultPrinter) return PrinterName; } return ""; }

Quite sure your example is "by design", and you are able to change the default printer - but it ONLY persists during the running of that program, so it don't effect every other program. However, this "advice" and point of mine is in the context of desktop software. So, you can change for your "current" application, and it will remain as selected, but upon exit of your application, the over all "OS" default printer is not changed, nor is it for word, and all the other programs running (hence by design).

However, the above posted code actually changes the OS default printer.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.