{"record":{"id":"d0c3d461a79da171","repo":"dotnet/wpf","slug":"printsystemexception-printserver-deleteconnection","errorCode":null,"errorMessage":"PrintSystemException.PrintServer.DeleteConnection","messagePattern":"PrintSystemException\\.PrintServer\\.DeleteConnection","errorType":"exception","errorClass":"PrintServerException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp","lineNumber":590,"sourceCode":"\n--*/\nbool\nLocalPrintServer::\nDisconnectFromPrintQueue(\n    String^ printQueuePath\n    )\n{\n    VerifyAccess();\n\n    bool    returnValue = false;\n\n    try\n    {\n        returnValue = PrintWin32Thunk::PrinterThunkHandler::ThunkDeletePrinterConnection(printQueuePath);\n    }\n    catch (InternalPrintSystemException^ internalException)\n    {\n        throw CreatePrintServerException(internalException->HResult, \"PrintSystemException.PrintServer.DeleteConnection\");\n    }\n\n    return returnValue;\n}\n\n/*++\n\nRoutine Name:\n\n    DisconnectFromPrintQueue\n\nRoutine Description:\n\n    Deletes a printer connection to a given printer.\n\nArguments:\n\n    printerPath   - \\\\server\\share or \\\\server\\printerName","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp#L572-L608","documentation":"Thrown when disconnecting from a network print queue by path fails. ThunkDeletePrinterConnection calls the Win32 DeletePrinterConnection API; any spooler error is wrapped in a PrintSystemException with the DeleteConnection key.","triggerScenarios":"Calling LocalPrintServer.DisconnectFromPrintQueue(printQueuePath) for a path that was never connected, is misspelled, or where the connection is held by another process/session the caller cannot modify.","commonSituations":"Trying to disconnect a queue that doesn't exist in the user's connections; deleting connections per-machine vs per-user confusion (connections are per-user); path casing/server alias mismatches (FQDN vs NetBIOS name).","solutions":["Check the connection exists first via GetPrintQueues / net use before calling DisconnectFromPrintQueue.","Use the exact same path string used when the connection was added (same server name form).","Remember connections are per-user: run in the same user context that created the connection.","Inspect the inner HResult for the specific Win32 error."],"exampleFix":"// before: blind disconnect\nserver.DisconnectFromPrintQueue(@\"\\\\printserver\\LaserJet\");\n// after: only disconnect existing connections\nif (server.GetPrintQueues().Any(q => q.FullName == @\"\\\\printserver\\LaserJet\"))\n    server.DisconnectFromPrintQueue(@\"\\\\printserver\\LaserJet\");","handlingStrategy":"validation","validationCode":"bool isConnected = server.GetPrintQueues().Any(q => q.FullName == printQueuePath);\nif (!isConnected) return; // nothing to disconnect","typeGuard":"static bool IsConnected(LocalPrintServer server, string path) =>\n    server.GetPrintQueues().Any(q => q.FullName == path);","tryCatchPattern":"try { server.DisconnectFromPrintQueue(path); }\ncatch (PrintSystemException ex)\n{\n    logger.LogWarning(\"Disconnect failed for {Path}: {HResult}\", path, ex.InnerException?.HResult);\n}","preventionTips":["Only disconnect connections you previously created in the same user session","Use the exact path string used at connect time","Treat disconnect failures as non-fatal in cleanup code"],"tags":["printing","network-printer","unc-path","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-22T01:17:13.364Z"}