{"record":{"id":"52556cd260254b53","repo":"dotnet/wpf","slug":"printsystemexception-printserver-setdefaultprinter","errorCode":null,"errorMessage":"PrintSystemException.PrintServer.SetDefaultPrinter","messagePattern":"PrintSystemException\\.PrintServer\\.SetDefaultPrinter","errorType":"exception","errorClass":"PrintServerException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp","lineNumber":1023,"sourceCode":"    {\n        for(int propertyIndex = 0;\n            propertyIndex < properties->Length;\n            propertyIndex++)\n        {\n            PrintProperty^  attributeValue = attributeValue = PropertiesCollection->GetProperty(properties[propertyIndex]);\n\n            //\n            // hase something table driven\n            //\n            if (attributeValue->Name->Equals(\"DefaultPrintQueue\"))\n            {\n                try\n                {\n                    PrintWin32Thunk::PrinterThunkHandler::ThunkSetDefaultPrinter(GetFullPrintQueueName((PrintQueue^)attributeValue->Value));\n                }\n                catch (InternalPrintSystemException^ internalException)\n                {\n                    throw CreatePrintServerException(internalException->HResult, \"PrintSystemException.PrintServer.SetDefaultPrinter\");\n                }\n            }\n        }\n   }\n\n}\n\n/*++\n\nRoutine Name:\n\n    Commit\n\nRoutine Description:\n\n    Commits the dirty attributes to server.\n\nArguments:","sourceCodeStart":1005,"sourceCodeEnd":1041,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp#L1005-L1041","documentation":"Thrown when setting LocalPrintServer.DefaultPrintQueue fails. The property setter calls ThunkSetDefaultPrinter with the full queue name; a Win32 failure (queue not found, access denied) is wrapped as this PrintSystemException.","triggerScenarios":"Assigning LocalPrintServer.DefaultPrintQueue = someQueue where the queue name cannot be resolved by the spooler (queue deleted, remote queue not connected, or the process lacks permission to change the user's default printer).","commonSituations":"Setting a default from a queue obtained on a different server/session; service or elevated process changing HKCU printer settings for another user; queue removed by group policy between enumeration and assignment.","solutions":["Confirm the queue exists and is connectable before assigning it (server.GetPrintQueues().Contains / FullName lookup).","For remote queues, connect to the queue first (ConnectToPrintQueue) before making it default.","Run in the target user's session; changing a default printer affects the current user's profile only.","Check the inner HResult (e.g. ERROR_FILE_NOT_FOUND, ERROR_ACCESS_DENIED) for the root cause."],"exampleFix":"// before\nserver.DefaultPrintQueue = someRemoteQueue; // may throw if not connected\n// after\nserver.ConnectToPrintQueue(someRemoteQueue.FullName);\nserver.DefaultPrintQueue = someRemoteQueue;","handlingStrategy":"validation","validationCode":"var exists = server.GetPrintQueues().Any(q => q.FullName == newDefault.FullName);\nif (!exists) throw new InvalidOperationException($\"Cannot set default: {newDefault.FullName} not found\");","typeGuard":"static bool CanSetDefault(LocalPrintServer server, PrintQueue q) =>\n    server.GetPrintQueues().Any(x => x.FullName == q.FullName) && !q.IsOffline;","tryCatchPattern":"try { server.DefaultPrintQueue = newDefault; }\ncatch (PrintSystemException ex)\n{\n    logger.LogError(ex.InnerException, \"Setting default printer {Name} failed\", newDefault.FullName);\n}","preventionTips":["Connect to remote queues before making them default","Only change defaults in the target user's interactive session","Re-check queue existence right before assignment"],"tags":["printing","default-printer","permissions","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"}