I am trying to get a list of all available printers, be they localy installed or network printers. to do this i used the following code which i found here how to get network printers
My Implementation
[HttpGet]
[Route("API/GetPrintersTest")]
public List<string> GetPrinters()
{
List<string> list = new List<string>();
// Use the ObjectQuery to get the list of configured printers
System.Management.ObjectQuery oquery =
new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
System.Management.ManagementObjectSearcher 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"] || (bool)mo["Local"])
{
if (pd.Name == "Name")
list.Add(pd.Name + ": " + mo[pd.Name]);
}
}
}
return list;
}
This works perfectly fine when i am debugging the WebAPI from visual studio but once i deploy my webapi and ask for the same request i only see the local printers and not the network printers.
Here is the result of running the WebAPI in debug mode from visual studio.
Name: Microsoft XPS Document Writer
Name: Microsoft Print to PDF
Name: Fax
Name: Brother MFC-J825DW Printer
Name: Brother HL-5450DN series Printer
Name: \sbs2011\RICOH_MAGABUR
Here is the result of running the published WebAPI with IIS.
Name: Microsoft XPS Document Writer
Name: Microsoft Print to PDF
Name: Fax
Name: Brother MFC-J825DW Printer
Name: Brother HL-5450DN series Printer
The only printer that's a network printer is \sbs2011\RICOH_MAGABUR
As you can see the lists are different. The only thing i could think of that could cause this is a lack of permissions for the web api. so i tried this to solve it using the folliwing link but to no avail. IIS Application pool permissions for web api
Is there anything i am missing here? did i set the permissions wrong or is there something else causing the issue.
Thanks in advance for your time and effort.