我想搜索网络中有多少台打印机。我已经检查了Installed printer属性,它给了我那些安装在我系统上的打印机列表。
我的网络中有两台以上的打印机,其中只有一台显示在列表中,因为它安装在我的系统上。
如何从网络获取所有打印机列表谁的驱动程序未安装在我的系统上或未连接到我的系统。
答案 0 :(得分:5)
我知道这篇文章很老了,但我一直在和同样的问题作斗争。
我最终设法解决了它,我希望下面的代码可以帮助某人:
using(var ds = new DirectorySearcher())
{
ds.SearchRoot = new DirectoryEntry("");
ds.Filter = "(|(&(objectCategory=printQueue)(name=*)))";
ds.PropertiesToLoad.Add("printername");
ds.PropertiesToLoad.Add("portname");
ds.PropertiesToLoad.Add("servername");
ds.PropertiesToLoad.Add("cn");
ds.PropertiesToLoad.Add("name");
ds.PropertiesToLoad.Add("printsharename");
ds.ReferralChasing = ReferralChasingOption.None;
ds.Sort = new SortOption("name", SortDirection.Descending);
using(var src = ds.FindAll())
{
foreach(SearchResult sr in src)
{
Console.WriteLine("------------------------------------");
Console.WriteLine(sr.GetDirectoryEntry().Name);
foreach (DictionaryEntry p in sr.Properties)
{
var propName = p.Key;
var propCollection = (ResultPropertyValueCollection)p.Value;
var propValue = propCollection.Count > 0 ? propCollection[0] : "";
Console.WriteLine(propName);
Console.WriteLine(propValue);
}
Console.WriteLine("------------------------------------");
}
}
}
如果您想要返回所有属性以查看可用的内容,那么只需注释掉ds.PropertiesToLoad行,这将为您提供完整列表。
答案 1 :(得分:0)
试试这个。
System.Management.ManagementScope objMS =
new System.Management.ManagementScope(ManagementPath.DefaultPath);
objMS.Connect();
SelectQuery objQuery = new SelectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher objMOS = new ManagementObjectSearcher(objMS, objQuery);
System.Management.ManagementObjectCollection objMOC = objMOS.Get();
foreach (ManagementObject Printers in objMOC)
{
if (Convert.ToBoolean(Printers["Local"])) // LOCAL PRINTERS.
{
cmbLocalPrinters.Items.Add(Printers["Name"]);
}
if (Convert.ToBoolean(Printers["Network"])) // ALL NETWORK PRINTERS.
{
cmbNetworkPrinters.Items.Add(Printers["Name"]);
}
}
}