gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:13:51 24 4
gpt4 key购买 nike

如下图所示,当我尝试检索所有打印机时,我只得到了 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

最佳答案

简单地说,

通过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

更多信息,请查看此article & WMI 打印机 Class

关于c# - 如何列出网络计算机上的所有打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10888201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com