如何列出网络计算机上的所有打印机

4
如下图所示,当我尝试检索所有打印机时,只得到了2个打印机。是否有一种方法可以使用PowerShell WMI或C#返回所有打印机(以便我可以将其翻译为powershell)?我已经尝试过System.Drawing.Printing.PrinterSettings.InstalledPrinters(参见how to get the list of all printers in computer - C# Winform),但也仅显示了2个条目。

enter image description here


你的问题不太清楚(疑问来自命令提示符截图)。你是想查找网络计算机上安装的打印机,而这台计算机不是你自己的吗? - Steve
是的,你说得对,Steve。我正在尝试在网络计算机上获取打印机,而不是在自己的计算机上。 - dance2die
刚刚在我的网络上尝试了你的 PowerShell 脚本,我可以获取到网络服务器上的所有打印机。可能只是权限问题吗?在这里,我是管理员,并且对该服务器上的打印机拥有完全权限。 - Steve
@Steve:我正在尝试弄清楚这是否是权限问题。我会尽快回复你。 - dance2die
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打印机


抱歉,我在问题中应该更具体地说明,我正在尝试检索安装在远程计算机上的打印机。比如说,如果我的计算机名称是comp1,那么我想列出计算机名为comp2的机器上的打印机列表,而不是列出安装在comp1上的打印机列表。 - dance2die
你现在有解决方案了吗?我也遇到了同样的问题:“比如我的计算机名是comp1,我想列出机器名为comp2的打印机列表,而不是安装在comp1上的打印机列表。” - iam user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接