{"record":{"id":"79a6d97c0c834047","repo":"dotnet/wpf","slug":"printsystemexception-printserver-addconnection","errorCode":null,"errorMessage":"PrintSystemException.PrintServer.AddConnection","messagePattern":"PrintSystemException\\.PrintServer\\.AddConnection","errorType":"exception","errorClass":"PrintServerException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp","lineNumber":509,"sourceCode":"\n--*/\nbool\nLocalPrintServer::\nConnectToPrintQueue(\n    String^ printQueuePath\n    )\n{\n    VerifyAccess();\n\n    bool    returnValue = false;\n\n    try\n    {\n        returnValue = PrintWin32Thunk::PrinterThunkHandler::ThunkAddPrinterConnection(printQueuePath);\n    }\n    catch (InternalPrintSystemException^ internalException)\n    {\n        throw CreatePrintServerException(internalException->HResult, \"PrintSystemException.PrintServer.AddConnection\");\n    }\n\n    return returnValue;\n}\n\n/*++\n\nRoutine Name:\n\n    ConnectToPrintQueue\n\nRoutine Description:\n\n    Creates a printer connection to a given printer.\n\nArguments:\n\n    printerPath   - \\\\server\\share or \\\\server\\printerName","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp#L491-L527","documentation":"Thrown by LocalPrintServer when connecting to a network print queue by path fails. ThunkAddPrinterConnection calls the Win32 AddPrinterConnection API; any failure (access denied, unreachable server, bad path) is converted to a PrintSystemException with the AddConnection key.","triggerScenarios":"Calling LocalPrintServer.ConnectToPrintQueue(printQueuePath) with a path the spooler cannot connect to: \\\\server\\printer name wrong, server offline, printer not shared, or the user lacks permission on the remote queue.","commonSituations":"Typo in UNC path; remote print server firewalled or renamed; domain credentials without 'Print' permission on the shared queue; VPN not connected when deploying to a client machine.","solutions":["Verify the UNC path exists and is reachable (test with `net use \\\\server\\printer`).","Confirm the user has print permission on the shared queue on the remote server.","Check network/VPN/firewall connectivity to the print server (SMB/RPC ports).","Inspect the inner exception HResult (e.g. 0x5 access denied, 0x709 unknown printer) for the specific cause."],"exampleFix":"// before\nserver.ConnectToPrintQueue(\"\\\\\\u200b\\\\oldserver\\\\LaserJet\");\n// after: validate reachability and handle failure\nvar path = @\"\\\\printserver\\LaserJet\";\nif (LocalPrintServer.GetDefaultPrintQueue() != null)\n{\n    try { server.ConnectToPrintQueue(path); }\n    catch (PrintSystemException ex) { /* ex.InnerException.HResult: check path/permissions */ }\n}","handlingStrategy":"validation","validationCode":"static bool CanReach(string uncPath)\n{\n    var parts = uncPath.TrimStart('\\\\').Split('\\\\');\n    if (parts.Length < 2) return false;\n    try { return System.Net.Dns.GetHostEntry(parts[0]) != null; }\n    catch { return false; }\n}","typeGuard":"static bool IsValidUncPath(string? path) =>\n    !string.IsNullOrWhiteSpace(path) && path.StartsWith(@\"\\\\\") && path.TrimStart('\\\\').Split('\\\\').Length >= 2;","tryCatchPattern":"try { server.ConnectToPrintQueue(path); }\ncatch (PrintSystemException ex)\n{\n    switch (ex.InnerException?.HResult)\n    {\n        case 0x00000005: throw new UnauthorizedAccessException(\"Access denied connecting to \" + path, ex);\n        case 0x00000709: throw new InvalidOperationException(\"Printer name not found: \" + path, ex);\n        default: throw;\n    }\n}","preventionTips":["Validate UNC paths with Dns/ping before connecting","Store print server names in config, not hard-coded literals","Test queue connectivity at app startup, fail fast with a clear message"],"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-21T21:30:21.729Z"}