-1

on my app (win10, WPF) i give the user the possibility to print a report.
BUT
when there is not-yet-configured printer on the LOCAL machine - it is impossible.
(of course that the printer is activated on the network, and by using settings -> add printer & sccaners it is found and installable)

i've searched the net for few days, looking for a way to:
1.
use ManagementObjectSearcher("SELECT * from Win32_Printer")
but got only localy installed printing options

2.

tried the following code - but i don't know the server or printer name
(i might know the network name where the user runs my app - but it can be any type of printer...)

using (ManagementClass win32Printer = new ManagementClass("Win32_Printer"))
{
    using (ManagementBaseObject inputParam = win32Printer.GetMethodParameters("AddPrinterConnection"))
    {
        // Replace <server_name> and <printer_name> with the actual server and
        // printer names.
        inputParam.SetPropertyValue("Name", "\\\\<server_name>\\<printer_name>");

        using (ManagementBaseObject result =
            (ManagementBaseObject)win32Printer.InvokeMethod("AddPrinterConnection", inputParam, null))
        {
            uint errorCode = (uint)result.Properties["returnValue"].Value;

            switch (errorCode)
            {
                case 0:
                    Console.Out.WriteLine("Successfully connected printer.");
                    break;
                case 5:
                    Console.Out.WriteLine("Access Denied.");
                    break;
                case 123:
                    Console.Out.WriteLine("The filename, directory name, or volume label syntax is incorrect.");
                    break;
                case 1801:
                    Console.Out.WriteLine("Invalid Printer Name.");
                    break;
                case 1930:
                    Console.Out.WriteLine("Incompatible Printer Driver.");
                    break;
                case 3019:
                    Console.Out.WriteLine("The specified printer driver was not found on the system and needs to be downloaded.");
                    break;
            }
        }
    }
}
  1. after installing the printer drive (from windows add printers & scanners) i got the mac address of the printer then even after un-installing i could get its' IP-Address (as suggested here)

none of the above helped me...

therefore i'm looking for any possible way to install the new printer programmatically - such as:

  1. launch PrintDialog with "add new printer" option
  2. open the windows settings window with the printers & scanners screen
    found that: Process.Start("ms-settings:printers");
  3. install the new printer using any of the above data
  4. do it in any other way.......... :)
RedHat
  • 167
  • 11
  • 1
    Why not just require that there is an installed printer? I'm not sure why you want to take over this functionality... – Ron Beyer May 13 '19 at 17:06
  • @RonBeyer - because every system i deliver will be in who-knows-what country/office etc. i don't know which printers are there and what networks... – RedHat May 14 '19 at 06:46
  • [This](https://learn.microsoft.com/en-us/dotnet/api/system.printing.enumeratedprintqueuetypes?view=netframework-4.8#examples) would be a good place to start. Use EnumeratedPrintQueueTypes to get the type of printers you want. – Sajith Sageer May 14 '19 at 07:08
  • thanks @SajithSageer... but for some reason when running this code on my computer (with 2 printers installed & onNote etc..) - there are no results (`printQueuesOnLocalServer.Count` = 0) – RedHat May 14 '19 at 07:57
  • 1
    EnumeratedPrintQueueTypes gives an intersection and not a union. So if you add both EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Shared it will look for printers that are both local and shared – Sajith Sageer May 14 '19 at 08:15

1 Answers1

0

another thing - as @SajithSageer suggested on the comments above (based on this link) :

i removed 1 of my 2 network printers, 1 HP & 1 Cannon - that both were found on my WiFi Network and installed from there as well, and the results were:
1. none of them is "SHARED" - they are always "LOCAL"
2. when the Cannon is connected - it is also appears on "EnableBidi"
3. EnumeratedPrintQueueTypes = 512 doesn't exists - but gives results :)

here is the code:

            for (int i = 0; i < 20; i++)
            {
                EnumeratedPrintQueueTypes[] enumerationFlags = { (EnumeratedPrintQueueTypes)Math.Pow(2,i)};

                LocalPrintServer printServer = new LocalPrintServer();

                //Use the enumerationFlags to filter out unwanted print queues
                PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(enumerationFlags);

                Console.WriteLine("These are your shared, local print queues:\t {0}\n---------------------------\n", enumerationFlags[0]);

                foreach (PrintQueue printer in printQueuesOnLocalServer)
                {
                    Console.WriteLine("\t" + printer.Name );
                }
                Console.WriteLine();
            }

here are the results:

These are your shared, local print queues:       Queued
---------------------------


These are your shared, local print queues:       DirectPrinting
---------------------------


These are your shared, local print queues:       4
---------------------------


These are your shared, local print queues:       Shared
---------------------------


These are your shared, local print queues:       Connections
---------------------------


These are your shared, local print queues:       32
---------------------------


These are your shared, local print queues:       Local
---------------------------

        OneNote
        Send To OneNote 2016
        Microsoft XPS Document Writer
        Microsoft Print to PDF
        HP872916 (HP OfficeJet Pro 7740 series)
        Fax - HP OfficeJet Pro 7740 series
        Fax

These are your shared, local print queues:       EnableDevQuery
---------------------------


These are your shared, local print queues:       KeepPrintedJobs
---------------------------


These are your shared, local print queues:       512
---------------------------

        Send To OneNote 2016
        Microsoft XPS Document Writer
        Microsoft Print to PDF
        HP872916 (HP OfficeJet Pro 7740 series)

These are your shared, local print queues:       WorkOffline
---------------------------


These are your shared, local print queues:       EnableBidi
---------------------------


These are your shared, local print queues:       RawOnly
---------------------------


These are your shared, local print queues:       PublishedInDirectoryServices
---------------------------


These are your shared, local print queues:       Fax
---------------------------

        Fax

These are your shared, local print queues:       TerminalServer
---------------------------


These are your shared, local print queues:       65536
---------------------------


These are your shared, local print queues:       PushedUserConnection
---------------------------


These are your shared, local print queues:       PushedMachineConnection
---------------------------


These are your shared, local print queues:       524288
---------------------------


Press enter to continue.
RedHat
  • 167
  • 11