How to get network printers in ASP.NET

I need to show printers installed on the network. I used the code below to show printers, but locally it displays network printers, but when placed in IIS it does not show a network printer (only local printers are shown).

code -1:

     ApplicationPrinter printer = new ApplicationPrinter();
     ddlPrinters.DataSource = printer.InstalledPrinters();
     ddlPrinters.DataBind();
     ddlPrinters.SelectedItem.Text = printer.DefaultZebraPrinter();

code -2:

    ddlPrinters.Items.Clear();

        try
        {
            // Use the ObjectQuery to get the list of configured printers
            ObjectQuery oquery =
                new ObjectQuery("SELECT * FROM Win32_Printer");

            ManagementObjectSearcher mosearcher =
                new ManagementObjectSearcher(oquery);

            ManagementObjectCollection moc = mosearcher.Get();

            foreach (ManagementObject mo in moc)
            {
                PropertyDataCollection pdc = mo.Properties;
                foreach (PropertyData pd in pdc)
                {
                    if ((bool)mo["Network"])
                    {
                        ddlPrinters.Items.Add(mo["Name"].ToString());
                        break;
                    }

                }
            }
        }
        catch (ManagementException ex)
        {
            string msgDesc = string.Empty;
            string script = string.Empty;
            msgDesc = ex.Message;
            script = "<script language=\"javascript\">alert('" + msgDesc + "'); </script>";
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "UserMessage", script, false);
        }

Here, if ((bool) mo ["Network"]) is used to display only network printers.

Is there a way to get network printers in ASP.NET?

+3
source share
2 answers

Adding this line to the web.config section does the trick. The user ID provided should reflect network printers.

<identity impersonate="true" userName="domain\user" password="password" />
+2
source

, win32_printer?

P.s. \\printername

0

Source: https://habr.com/ru/post/1791934/


All Articles