{"record":{"id":"9def609c17c4552e","repo":"dotnet/wpf","slug":"printsystemexception-printserver-namecannotchange","errorCode":null,"errorMessage":"PrintSystemException.PrintServer.NameCannotChange","messagePattern":"PrintSystemException\\.PrintServer\\.NameCannotChange","errorType":"exception","errorClass":"PrintServerException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/PrintServer.cpp","lineNumber":2247,"sourceCode":"set(\n    String^ name\n    )\n{\n    try\n    {\n        System::Threading::Monitor::Enter(SyncRoot);\n\n        if (IsInternallyInitialized)\n        {\n            PropertiesCollection->GetProperty(\"Name\")->IsInternallyInitialized = true;\n\n            PrintSystemObject::Name::set(name);\n            PropertiesCollection->GetProperty(\"Name\")->Value = name;\n        }\n        else\n        {\n            System::Threading::Monitor::Exit(SyncRoot);\n            throw CreatePrintServerException(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED),\n                                             \"PrintSystemException.PrintServer.NameCannotChange\"\n                                             );\n        }\n    }\n    __finally\n    {\n        IsInternallyInitialized = false;\n        System::Threading::Monitor::Exit(SyncRoot);\n    }\n}\n\n/*++\n\nRoutine Name:\n\n    CreatePropertiesDelegates\n\nRoutine Description:","sourceCodeStart":2229,"sourceCodeEnd":2265,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Printing/CPP/src/PrintServer.cpp#L2229-L2265","documentation":"This error is thrown by System.Printing's PrintServer.Name property setter when you attempt to rename a PrintServer object. A print server's name is fixed by the spooler/OS and cannot be changed through this API; when the new name differs from the current one, the setter releases its lock and throws a PrintSystemException carrying HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) with lookup key PrintSystemException.PrintServer.NameCannotChange. It exists to signal an inherently unsupported operation rather than a transient failure.","triggerScenarios":"Assigning a different value to PrintServer.Name (e.g. printServer.Name = \"NewServer\") — the C++ setter in PrintServer.cpp detects the name change and throws ERROR_NOT_SUPPORTED. Setting Name to the same value does not throw.","commonSituations":"Scripts or admin tools that generically set properties on print-system objects reused across PrintQueue and PrintServer types; renaming code copied from PrintQueue examples applied to a PrintServer; attempts to redirect to a remote print server by changing Name instead of constructing a new PrintServer.","solutions":["Do not rename the PrintServer; create a new PrintServer with the desired name instead: new PrintServer(@\"\\\\ServerName\") and discard the old instance.","If the goal is to target a remote server, pass the server path to the PrintServer constructor rather than mutating Name.","If the assignment is generated by generic property-setting code, special-case the Name property for PrintServer and skip it.","Wrap the Name setter in a try/catch for PrintSystemException only if the rename is best-effort and can be ignored."],"exampleFix":"// before\nPrintServer server = new PrintServer();\nserver.Name = \"\\\\\\\\OtherServer\"; // throws PrintSystemException.PrintServer.NameCannotChange\n// after\nPrintServer server = new PrintServer(\"\\\\\\\\OtherServer\");","handlingStrategy":"try-catch","validationCode":"if (server.Name != newName)\n    throw new NotSupportedException(\"PrintServer.Name cannot be changed; construct a new PrintServer instead.\");","typeGuard":"static bool CanRename(PrintServer s, string newName) => s != null && string.Equals(s.Name, newName, StringComparison.Ordinal);","tryCatchPattern":"try { server.Name = newName; }\ncatch (PrintSystemException ex) when (ex.Message.Contains(\"NameCannotChange\")) {\n    server = new PrintServer(newName);\n}","preventionTips":["Treat PrintServer.Name as read-only; always construct a new PrintServer to target a different server.","Never reuse a generic property-assignment loop across PrintQueue and PrintServer objects without special-casing Name.","Document in shared print-helper code that server rename is unsupported by the spooler."],"tags":["printing","wpf","unsupported-operation","printserver"],"backgroundTag":"unsupported-operation","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"}