{"record":{"id":"f89755303c5ed183","repo":"dotnet/wpf","slug":"printsystemexception-printserver-getdefaultprinter","errorCode":null,"errorMessage":"PrintSystemException.PrintServer.GetDefaultPrinter","messagePattern":"PrintSystemException\\.PrintServer\\.GetDefaultPrinter","errorType":"exception","errorClass":"PrintServerException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp","lineNumber":940,"sourceCode":"                    else\n                    {\n                        //\n                        // This means that this is not a Connection.\n                        // Could be something else other than a local\n                        // printer, but we don't care at this stage\n                        //\n                        targetPrintServer = this;\n                    }\n                    attributeValue->Value = gcnew PrintQueue(targetPrintServer,\n                                                             defaultPrinterName);\n                }\n\n                attributeValue->IsInternallyInitialized = false;\n\n            }\n            catch (InternalPrintSystemException^ internalException)\n            {\n                throw CreatePrintServerException(internalException->HResult, \"PrintSystemException.PrintServer.GetDefaultPrinter\");\n            }\n        }\n    }\n}\n\n/*++\n\nRoutine Name:\n\n    GetUnInitializedData\n\nRoutine Description:\n\n    Initialize an array of properties with data from the Spooler service.\n\nArguments:\n\n    properties - array of string representing the properties to be initialized","sourceCodeStart":922,"sourceCodeEnd":958,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp#L922-L958","documentation":"Thrown when LocalPrintServer fails to read the default printer while initializing property attributes. The Win32 GetDefaultPrinter call (via the spooler) failed, and the InternalPrintSystemException is rethrown as a PrintSystemException with the GetDefaultPrinter key.","triggerScenarios":"Accessing LocalPrintServer.DefaultPrintQueue (or refresh of the DefaultPrintQueue attribute) on a machine where no default printer is set, or where GetDefaultPrinter fails due to spooler issues or permissions.","commonSituations":"Fresh Windows installs / server cores with no printers configured; service accounts and IIS app pools with no user printer profile; Spooler service stopped; RDS sessions without printer redirection.","solutions":["Check whether a default printer exists (GetDefaultPrinter Win32 / Settings > Printers) before accessing DefaultPrintQueue.","Start the Print Spooler service if it is stopped.","Run the code in an interactive user session that has printer configuration, not a bare service context.","Set a default printer programmatically or manually, or handle the null/no-default case explicitly."],"exampleFix":"// before\nPrintQueue queue = new LocalPrintServer().DefaultPrintQueue; // throws when none set\n// after\nvar server = new LocalPrintServer();\nvar queue = server.GetPrintQueues().FirstOrDefault();\nif (queue != null) server.DefaultPrintQueue = queue;\nvar target = server.DefaultPrintQueue ?? queue;","handlingStrategy":"fallback","validationCode":"static bool HasDefaultPrinter()\n{\n    var ps = new LocalPrintServer();\n    return ps.GetPrintQueues().Any();\n}","typeGuard":"static bool TryGetDefaultQueue(LocalPrintServer server, out PrintQueue? queue)\n{\n    queue = server.GetPrintQueues().FirstOrDefault();\n    return queue != null;\n}","tryCatchPattern":"PrintQueue? target;\ntry { target = server.DefaultPrintQueue; }\ncatch (PrintSystemException)\n{\n    target = server.GetPrintQueues().FirstOrDefault(); // fall back to first available\n}","preventionTips":["Always provide a fallback when no default printer is configured","Detect headless/service sessions and skip print features","Prompt users to set a default printer on first run"],"tags":["printing","default-printer","spooler","wpf"],"backgroundTag":"resource-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}