我正在尝试在C#中编写一个方法,以清空打印队列中的所有项目。以下是我的代码:
LocalPrintServer localPrintServer = new LocalPrintServer(PrintSystemDesiredAccess.AdministratePrinter);
PrintQueue printQueue = localPrintServer.GetPrintQueue(printerName);
if (printQueue.NumberOfJobs > 0)
{
printQueue.Purge();
}
当这段代码运行时,在localPrintServer构造函数上,应用程序会抛出此错误:“创建PrintServer对象时发生异常。Win32错误:拒绝访问。”
该构造函数有几个重载版本(包括不发送任何参数)。尝试使用其中任何一个,我可以通过该行,但是当我到达printQueue.Purge()调用时,我得到与上述相同的访问被拒绝信息。
请提供建议以解决此问题。我可以手动从我的计算机中删除打印作业。我不确定应用程序是否以我拥有的相同访问权限运行,也不知道如何检查。