是否有直接的方法来枚举 .NET 中所有可见的网络打印机?目前,我正在展示 PrintDialog 以允许用户选择打印机。问题在于,本地打印机也会显示(以及 XPS Document Writer 等)。如果我可以自己枚举网络打印机,我可以显示一个仅包含这些打印机的自定义对话框。
谢谢!!
最佳答案
- 从
LocalPrintServer.DefaultPrintQueue
获取默认打印机 - 从
PrinterSettings.InstalledPrinters
获取已安装的打印机(从用户的角度来看) - 枚举列表:
- 任何以
\\
开头的打印机都是网络打印机 - 所以使用new PrintServer("\\UNCPATH").GetPrintQueue("QueueName")
- 任何不以
\\
开头的打印机都是本地打印机,因此使用LocalPrintServer.GetQueue("Name")
获取它
- 您可以通过比较
FullName
属性来查看哪个是默认值。
注意:网络打印机可以是 LocalPrintServer.DefaultPrintQueue
中的默认打印机,但不会出现在 LocalPrintServer.GetPrintQueues()
// get available printers
LocalPrintServer printServer = new LocalPrintServer();
PrintQueue defaultPrintQueue = printServer.DefaultPrintQueue;
// get all printers installed (from the users perspective)he t
var printerNames = PrinterSettings.InstalledPrinters;
var availablePrinters = printerNames.Cast<string>().Select(printerName =>
{
var match = Regex.Match(printerName, @"(?<machine>\\\\.*?)\\(?<queue>.*)");
PrintQueue queue;
if (match.Success)
{
queue = new PrintServer(match.Groups["machine"].Value).GetPrintQueue(match.Groups["queue"].Value);
}
else
{
queue = printServer.GetPrintQueue(printerName);
}
var capabilities = queue.GetPrintCapabilities();
return new AvailablePrinterInfo()
{
Name = printerName,
Default = queue.FullName == defaultPrintQueue.FullName,
Duplex = capabilities.DuplexingCapability.Contains(Duplexing.TwoSidedLongEdge),
Color = capabilities.OutputColorCapability.Contains(OutputColor.Color)
};
}).ToArray();
DefaultPrinter = AvailablePrinters.SingleOrDefault(x => x.Default);
关于c# - 是否有 .NET 方法来枚举所有可用的网络打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1018001/