dotnet/wpf · error · PrintServerException

PrintSystemException.PrintServer.AddConnection

Error message

PrintSystemException.PrintServer.AddConnection

What it means

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.

Solutions

  1. Verify the UNC path exists and is reachable (test with `net use \\server\printer`).
  2. Confirm the user has print permission on the shared queue on the remote server.
  3. Check network/VPN/firewall connectivity to the print server (SMB/RPC ports).
  4. Inspect the inner exception HResult (e.g. 0x5 access denied, 0x709 unknown printer) for the specific cause.

Example fix

// before
server.ConnectToPrintQueue("\\\u200b\\oldserver\\LaserJet");
// after: validate reachability and handle failure
var path = @"\\printserver\LaserJet";
if (LocalPrintServer.GetDefaultPrintQueue() != null)
{
    try { server.ConnectToPrintQueue(path); }
    catch (PrintSystemException ex) { /* ex.InnerException.HResult: check path/permissions */ }
}
Defensive patterns

Strategy: validation

Validate before calling

static bool CanReach(string uncPath)
{
    var parts = uncPath.TrimStart('\\').Split('\\');
    if (parts.Length < 2) return false;
    try { return System.Net.Dns.GetHostEntry(parts[0]) != null; }
    catch { return false; }
}

Type guard

static bool IsValidUncPath(string? path) =>
    !string.IsNullOrWhiteSpace(path) && path.StartsWith(@"\\") && path.TrimStart('\\').Split('\\').Length >= 2;

Try / catch

try { server.ConnectToPrintQueue(path); }
catch (PrintSystemException ex)
{
    switch (ex.InnerException?.HResult)
    {
        case 0x00000005: throw new UnauthorizedAccessException("Access denied connecting to " + path, ex);
        case 0x00000709: throw new InvalidOperationException("Printer name not found: " + path, ex);
        default: throw;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/79a6d97c0c834047. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/LocalPrintServer.cpp:509

--*/
bool
LocalPrintServer::
ConnectToPrintQueue(
    String^ printQueuePath
    )
{
    VerifyAccess();

    bool    returnValue = false;

    try
    {
        returnValue = PrintWin32Thunk::PrinterThunkHandler::ThunkAddPrinterConnection(printQueuePath);
    }
    catch (InternalPrintSystemException^ internalException)
    {
        throw CreatePrintServerException(internalException->HResult, "PrintSystemException.PrintServer.AddConnection");
    }

    return returnValue;
}

/*++

Routine Name:

    ConnectToPrintQueue

Routine Description:

    Creates a printer connection to a given printer.

Arguments:

    printerPath   - \\server\share or \\server\printerName

View on GitHub (pinned to 81131a70a4)