问题
I want to search how many printers are available in network. I have checked with Installed printer property and it gives me those printer list which are installed on my system.
I have more than two printers in my network where only one showing in list because it installed on my System.
How to get all printer list from Network who's drivers are not installed on my system or not connected to my system.
回答1:
I know this post is quite old, but I've been battling with the same issue.
I eventually managed to solve it and I hope the code below helps someone:
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("------------------------------------");
}
}
}
If you want to return all the properties to see what's available then just comment out the ds.PropertiesToLoad lines and that will give you the full list.
回答2:
try this.
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"]);
}
}
}
来源:https://stackoverflow.com/questions/19902379/how-to-get-printers-from-network-which-is-not-installed-in-my-system