This problem is caused because the GetPrintQueue
method is slightly evil, as it does not allow you to pass at the right access level. When using your code, you connect to the print server with AdministratePrinter
rights (which makes no sense) and connect to the print queue with default user rights. Thus, the operation will fail even if everyone has administrator rights in the print queue. A.
To fix this, use the constructor for PrintQueue
instead to specify the correct access level:
using (PrintServer ps = new PrintServer()) { using (PrintQueue pq = new PrintQueue(ps, printerName, PrintSystemDesiredAccess.AdministratePrinter)) { pq.Purge(); } }
This can still cause permission errors if you are not working in the context of a member of the Administrators group (or if you are not running with elevated permissions), so surrounding it with a try / catch block is a good idea for production code.
mdb
source share