Reputation: 21
I have written the following code it didnot get the Remote printers name Can any one please help me?
var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM
Win32_Printer");
var results = searcher.Get();
IList<ManagementBaseObject> printers = new List<ManagementBaseObject>();
`enter code here`
foreach (var printer in results) `enter code here`{
if ((bool)printer["Network"]) `enter code here`{`enter code here`
printers.Add(printer);`enter code here`
}
}
Upvotes: 2
Views: 10671
Reputation: 6528
If you are looking to find list of all Installed Network Printers then you can simply use the following function.
Try to run the following in by creating a Console Application.
You need to reference System.Printing Namespace. You need to manually add reference to its library by right-clicking on Add References and checking the same as shown below in the snapshot.
Once you are done with that then use the following code for your console application. I have added code here to show BOTH Network and Local Printers. You can pick your particular selection.
using System.Printing;
namespace GetPrinters
{
class Program
{
static void Main(string[] args)
{
var server = new PrintServer();
Console.WriteLine("Listing Shared Printers");
var queues = server.GetPrintQueues(new[]
{ EnumeratedPrintQueueTypes.Shared, EnumeratedPrintQueueTypes.Connections });
foreach (var item in queues)
{
Console.WriteLine(item.FullName);
}
Console.WriteLine("\nListing Local Printers Now");
queues = server.GetPrintQueues(new[]
{ EnumeratedPrintQueueTypes.Local });
foreach (var item in queues)
{
Console.WriteLine(item.FullName);
}
Console.ReadLine();
}
}
}
Upvotes: 4
Reputation: 5715
I think you have to update your code like below.
private void btnGetPrinters_Click(object sender, EventArgs e)
{
// Use the ObjectQuery to get the list of configured printers
var oquery = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
var mosearcher = new System.Management.ManagementObjectSearcher(oquery);
System.Management.ManagementObjectCollection moc = mosearcher.Get();
foreach (ManagementObject mo in moc)
{
System.Management.PropertyDataCollection pdc = mo.Properties;
foreach (System.Management.PropertyData pd in pdc)
{
if ((bool)mo["Network"])
{
cmbPrinters.Items.Add(mo[pd.Name]);
}
}
}
}
I copied it from Retrieve a List of Installed Printers on the Network Using WMI, C# and VB.NET
Upvotes: 1