1

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.

Viencent
  • 41
  • 1
  • 4
  • Network printers are similar to mapped drives which binds to your user session. However, IIS and its worker processes run in system session and there exists session isolation. – Lex Li Nov 16 '17 at 12:53
  • Is there a way i can acces the administrator session with my web api? I would assume the administrator has all the available printers in the session. – Viencent Nov 16 '17 at 12:55
  • Please learn session isolation via Google. Unfortunately administrators are not with endless power in such cases. You just cannot jump across the border. If you do want to make the Web API works in user session, make it a self hosting app. – Lex Li Nov 16 '17 at 13:47
  • I will look into the session isolation and the self hosting. The self hosting sounds acutally very interesting and might solf some other issues i have had. Thanks for this advice – Viencent Nov 16 '17 at 13:58

0 Answers0