Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Me and a colleague have tried lots of stuff to find a solution for this and we figured this worked: </p> <pre><code>private string[] GetAvailablePrinters() { var installedPrinters = new string[PrinterSettings.InstalledPrinters.Count]; PrinterSettings.InstalledPrinters.CopyTo(installedPrinters, 0); var printers = new List&lt;string&gt;(); var printServers = new List&lt;string&gt;(); var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"); foreach (var printer in searcher.Get()) { var serverName = @"\\" + printer["SystemName"].ToString().TrimStart('\\'); if (!printServers.Contains(serverName)) printServers.Add(serverName); } foreach (var printServer in printServers) { var server = new PrintServer(printServer); try { var queues = server.GetPrintQueues(); printers.AddRange(queues.Select(q =&gt; q.Name)); } catch (Exception) { // Handle exception correctly } } return printers.ToArray(); } </code></pre> <p>The trick is that when a printserver is not available, GetPrintQueues will throw some specific exception. By only adding the printers that don't throw such an exception, we get a list of all the connected printers. This doesn't check if a printer is turned on/off because that actually doesn't matter. If it is turned off, the document will just be placed in the print queue and it can be printed later on.</p> <p>I hope this helps others who bump into this problem.</p> <p><strong>Sidenote:</strong> The reason I decided not to catch that specific exception, is because I would have to reference a dll just for that exception.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload