1

A line similar to the following threw the above exception:

PrintServer ps = new PrintServer(@"\\prntsrv");

When I use "Run..." on Windows the address above does work and take me to the list of print jobs so why does the line of code not work?

shwartz
  • 631
  • 3
  • 13
  • 29

2 Answers2

0

Apparently, the address \\prntsrv was a DNS alias to \\prntserv and the PrintServer constructor was unable to deal with it. To get around this issue I used the following code (I could also use just the code in the catch block instead and it would work, but preferred not to):

        try
        {
            // Create object to monitor the printer queue
            PrintServer printServer = new PrintServer(serverPath);
            mPrintQueue = printServer.GetPrintQueue(printerName);
        }
        catch (PrintServerException ex)
        {
            // If the problem might be creating the server because the name is an alias
            if (ex.Message.Contains("printer name is invalid."))
            {
                string actualServerHostname = "\\\\" + Dns.GetHostEntry(serverPath.TrimStart('\\')).HostName;

                // Create object to monitor the printer queue
                PrintServer printServer = new PrintServer(actualServerHostname);
                mPrintQueue = printServer.GetPrintQueue(printerName);

                // Write to log about the use of a different address
            }
            else
            {
                throw;
            }
        }
shwartz
  • 631
  • 3
  • 13
  • 29
0

hey i was facing similar issue, this is what i observed and made following changes, just try and let me know.

This issue was occuring due to windows feature/role "Print and Document service" is missing on the system. This role is required for managing multiple printers or print servers and migrating printers to and from other windows servers.

To add the role Go To Control Panel->Turn windows feature on or off->click on check box "Print and Document Service"->install.

See with network administrator for installing this rule if you unable to add it.

After adding the role you can able to create print server object and get the all the printqueues on respective server.

Sagar
  • 399
  • 4
  • 11
  • I don't think I can test it any time soon, if at all. Anyway, my current setting is: SOME-ON Print and Document Services (ON Internet Printing Client, OFF LPD Print Service, OFF LPR Port Monitor, OFF Scan Management, ON Windows Fax and Scan). I guess the LPD Print service should be on for this to work, but I'm not sure I could have it turned on on the customer's side with our installer. – shwartz Apr 10 '14 at 11:14