4

如下图所示,当我尝试检索所有打印机时,我只有 2 台打印机。

有没有办法使用 PowerShell WMI 或 C# 返回所有打印机(以便我可以在 powershell 中翻译它)?

我已经尝试过System.Drawing.Printing.PrinterSettings.InstalledPrinters(参考how to get the list of all printers in computer - C# Winform),但也只显示了 2 个条目。

在此处输入图像描述

4

1 回答 1

3

简单地,

通过System.Drawing.Printing

foreach (String printer in PrinterSettings.InstalledPrinters)
{
    Console.WriteLine(printer.ToString()+Environment.NewLine);
} 

通过WMI

public static void AvailablePrinters()
{
 oManagementScope = new ManagementScope(ManagementPath.DefaultPath);
 oManagementScope.Connect();

 SelectQuery oSelectQuery = new SelectQuery();
 oSelectQuery.QueryString = @"SELECT Name FROM Win32_Printer";

 ManagementObjectSearcher oObjectSearcher = 
    new ManagementObjectSearcher(oManagementScope, @oSelectQuery);
 ManagementObjectCollection oObjectCollection = oObjectSearcher.Get();

 foreach (ManagementObject oItem in oObjectCollection)
 {

 Console.WriteLine("Name : " + oItem["Name"].ToString()+ Environment.NewLine);
 }
}

通过PowerShell

Get-WMIObject -class Win32_Printer -computer $printserver | Select Name,DriverName,PortName

有关更多信息,请查看本文和 WMI 打印机

于 2012-06-04T21:04:00.923 回答