Comparing PrintQueue to determine default print queue

1.2k views Asked by At

I am retrieving the default print queues thanks to the help of this question. I am also able to determine the DefaultPrintQueue

But how does one properly determine what print queue in the list of print queues is equal to the DefaultPrintQueue?

I've tried:

var dq = LocalPrintServer.GetDefaultPrintQueue();
foreach(PrintQueue pq in pqcOnLocalServer)
{
    if(pq.Equals(dq))
    {
        System.Console.WriteLine("Found default"); 
    }
}

but the two objects obviously won't be the same. I would then assume I could compare properties of each PrintQueue with the default, but what properties should be used to determine, 100%, that the two PrintQueues are referring to the same PrintQueue?

2

There are 2 answers

0
Kcvin On BEST ANSWER

This question might have done well on Expert Exchange, or Server Exchange. What I've found is that a print server will not allows printers on the server which have existing names already on the printer server. With that being said, a printer must have a unique name per server.

With that being said, a user must be careful to not only compare printer names to ensure that they are unique, but they must also compare the printer server that they are on. For example, when enumerating connected a printers. A computer could be connected to two print servers where there is a \\PRNTSRVR1\HQ_LaserJet01 and \\PRNTSRVR2\HQ_LaserJet01; so checking the connected server is important too.

3
Jamleck On

Try and use the LocalPrintServer.DefaultPrintQueue property to get the default print queue and compare the PrintQueue.FullName. This negates the need to iterate through the LocalPrintServer PrintQueueCollection.

LocalPrintServer printServer = new LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer);
PrintQueue pq = printServer.DefaultPrintQueue;

PrintQueue dq = LocalPrintServer.GetDefaultPrintQueue();

if (dq != null && pq.FullName.Equals(dq.FullName))
{
   Console.WriteLine("Found default print Queue: {0}", dq.FullName);
}

If you still need to iterate through the LocalPrintServer PrintQueueCollection you can try the implementation below.

LocalPrintServer printServer = new LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer);

PrintQueue dq = LocalPrintServer.GetDefaultPrintQueue();

foreach (PrintQueue pq in printServer.GetPrintQueues())
{
    if (dq != null && pq.FullName.Equals(dq.FullName))
    {
         Console.WriteLine("Found default print Queue: {0}", dq.FullName);
    }
}