{"record":{"id":"af435daaeec0a1b0","repo":"jstedfast/MailKit","slug":"cannot-rename-a-folder-using-itself-as-the-new-parent-folder","errorCode":null,"errorMessage":"Cannot rename a folder using itself as the new parent folder.","messagePattern":"Cannot rename a folder using itself as the new parent folder\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":1150,"sourceCode":"\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ImapCommandException\">\n\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override Task<IMailFolder?> CreateAsync (string name, IEnumerable<SpecialFolder> specialUses, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tvar ic = QueueCreateCommand (name, specialUses, cancellationToken, out var encodedName);\n\n\t\t\treturn CreateAsync (ic, encodedName, true, cancellationToken);\n\t\t}\n\n\t\tImapCommand QueueRenameCommand (IMailFolder parent, string name, CancellationToken cancellationToken, out string encodedName)\n\t\t{\n\t\t\tif (parent == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (parent));\n\n\t\t\tif (object.ReferenceEquals (parent, this))\n\t\t\t\tthrow new ArgumentException (\"Cannot rename a folder using itself as the new parent folder.\", nameof (parent));\n\n\t\t\tif (parent is not ImapFolder || ((ImapFolder) parent).Engine != Engine)\n\t\t\t\tthrow new ArgumentException (\"The parent folder does not belong to this ImapClient.\", nameof (parent));\n\n\t\t\tif (name == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (name));\n\n\t\t\tif (!ImapEngine.IsValidMailboxName (name, DirectorySeparator))\n\t\t\t\tthrow new ArgumentException (\"The name is not a legal folder name.\", nameof (name));\n\n\t\t\tif (IsNamespace || (Attributes & FolderAttributes.Inbox) != 0)\n\t\t\t\tthrow new InvalidOperationException (\"Cannot rename this folder.\");\n\n\t\t\tCheckState (false, false);\n\n\t\t\tstring newFullName;\n\n\t\t\tif (!string.IsNullOrEmpty (parent.FullName))","sourceCodeStart":1132,"sourceCodeEnd":1168,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L1132-L1168","documentation":"QueueRenameCommand validates that the new parent folder for a rename is not the folder itself; renaming a folder under itself is meaningless in IMAP and would create a cycle. MailKit throws ArgumentException with paramName 'parent' before any network traffic.","triggerScenarios":"Calling folder.Rename(folder, \"newname\") — i.e. passing the same IMailFolder instance as both the folder being renamed and the destination parent (object.ReferenceEquals(parent, this)).","commonSituations":"Generic folder-management UIs where 'move to folder' lets the user pick the currently selected folder; code that computes the destination parent from the same variable as the source.","solutions":["Guard before calling: if (!ReferenceEquals(parent, folder)) then rename","Choose the correct destination parent (usually folder.ParentFolder) instead of the folder itself","Return a friendly validation message in UIs when the destination equals the source"],"exampleFix":"// before\nfolder.Rename(folder, \"Renamed\");\n// after\nif (!object.ReferenceEquals(folder, newParent))\n    folder.Rename(newParent, \"Renamed\");","handlingStrategy":"validation","validationCode":"if (ReferenceEquals(sourceFolder, destinationParent))\n    throw new ArgumentException(\"Destination parent must differ from the folder being renamed.\");\nsourceFolder.Rename(destinationParent, newName);","typeGuard":null,"tryCatchPattern":"try {\n    folder.Rename(parent, name);\n} catch (ArgumentException ex) when (ex.ParamName == \"parent\") {\n    // surface: pick a different destination folder\n}","preventionTips":["In move/rename UIs, filter the currently selected folder out of destination pickers","Never reuse the same variable for source and destination parent"],"tags":["imap","mailkit","argument-validation","rename"],"backgroundTag":"invalid-argument-value","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}