0

The code below is a simple function that I wrote to check the status of a local printer device connected to the computer through USB cable:

        public static PrinterFailureType CheckPrinterStatus()
        {
            PrinterFailureType returnStatus = PrinterFailureType.None;

            try
            {
                using (PrintServer ps = new PrintServer())
                {

                    using (PrintQueue printQueue = ps.GetPrintQueue("examplePrinter"))
                    {
                        returnStatus = CheckStatus(printQueue);
                    }
                }
            } 
            catch (PrintQueueException)
            {
                returnStatus = PrinterFailureType.NotAvailable;
            }
        
            return returnStatus;
        }

During testing, I came across a few instances in which the software randomly threw the "Win32 error: The printer name is invalid." exception when calling the PrintServer() empty constructor. The Microsoft documentation of the constructor did not state any potential exception. My understanding of the class is if I instantiate a PrintServer class instance using the empty constructor, the class will be initialized using properties (e.g. name) from a local print server (which should be the computer?). Therefore, I am not sure why the software will throw this exception randomly. I did notice a pattern in which all occurrences of the exception took place at application startup after the computer was restarted. I suspected the cause of the issue might be related to properties of local print server not being available after the computer restart, but I don't have access to the internal logic of the PrintServer class to confirm that.

Does anyone has similar experience with this issue? Can you share your experience or feedback regarding it? Any help is appreciated!

zlallen
  • 11
  • 1
  • Have you gone though all these similar issues to determine if any apply to you https://www.google.com/search?q=PrintServer+the+printer+name+is+invalid+&ei=gYmEYaboJZKR9QPmpLn4BQ&oq=PrintServer+the+printer+name+is+invalid – TheGeneral Nov 05 '21 at 01:33
  • I found this helped me with a similar issue (same exception but I didn't make a connection between the problem and reboots as you have): https://stackoverflow.com/a/22981040/1848953 – AlanK Nov 05 '21 at 07:06

0 Answers0