C#获取网络共享打印机队列
var server = new LocalPrintServer();
var queues = server.GetPrintQueues();
//通过以上这种方式并不能获取到网络共享打印机,在进行尝试后使用以下方式成功获取
var allQueueList = GetPrintersWinSever();//获取全部打印机名称,包含共享打印机
var localQueueList = queues.Select(a => a.FullName).ToList();
foreach (var item in allQueueList)
{
if (!localQueueList.Contains(item))//遍历获取共享打印机的名称
{
//通过共享打印机的名称创建打印队列
PrintServer printServer = new PrintServer(item);
queues.Add(printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections }).First());
}
}
private static List<string> GetPrintersWinSever()
{
PrintDocument prtdoc = new PrintDocument();
List<string> lstPrinterName = new List<string>();
foreach (string strprinter in PrinterSettings.InstalledPrinters)
{
lstPrinterName.Add(strprinter);
}
return lstPrinterName;
}