{"record":{"id":"045a11057c086b98","repo":"jstedfast/MailKit","slug":"cannot-create-child-folders","errorCode":null,"errorMessage":"Cannot create child folders.","messagePattern":"Cannot create child folders\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolder.cs","lineNumber":838,"sourceCode":"\n\t\t\t\tEngine.OnFolderCreated (folder);\n\t\t\t}\n\n\t\t\treturn folder;\n\t\t}\n\n\t\tImapCommand QueueCreateCommand (string name, bool isMessageFolder, CancellationToken cancellationToken, out string encodedName)\n\t\t{\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\tCheckState (false, false);\n\n\t\t\tif (!string.IsNullOrEmpty (FullName) && DirectorySeparator == '\\0')\n\t\t\t\tthrow new InvalidOperationException (\"Cannot create child folders.\");\n\n\t\t\tvar fullName = !string.IsNullOrEmpty (FullName) ? FullName + DirectorySeparator + name : name;\n\t\t\tencodedName = Engine.EncodeMailboxName (fullName);\n\t\t\tvar createName = encodedName;\n\n\t\t\tif (!isMessageFolder && Engine.QuirksMode != ImapQuirksMode.GMail)\n\t\t\t\tcreateName += DirectorySeparator;\n\n\t\t\treturn Engine.QueueCommand (cancellationToken, null, \"CREATE %S\\r\\n\", createName);\n\t\t}\n\n\t\tMailboxIdResponseCode? ProcessCreateResponse (ImapCommand ic)\n\t\t{\n\t\t\tProcessResponseCodes (ic, null);\n\n\t\t\tif (ic.Response != ImapCommandResponse.Ok && ic.GetResponseCode (ImapResponseCodeType.AlreadyExists) == null)\n\t\t\t\tthrow ImapCommandException.Create (\"CREATE\", ic);\n","sourceCodeStart":820,"sourceCodeEnd":856,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolder.cs#L820-L856","documentation":"This InvalidOperationException is thrown by QueueCreateCommand when attempting to create a child folder under a parent whose DirectorySeparator is '\\0' - meaning the server (or this folder) does not support a hierarchy separator, so child folders cannot exist under it.","triggerScenarios":"Calling childFolder.CreateFolder(name) where childFolder has a non-empty FullName but DirectorySeparator == '\\0' - typical when the folder was opened without its children (children collection not loaded) or the server reports no separator for that mailbox.","commonSituations":"Creating subfolders under a folder whose separator is unknown because the folder was never opened/its children not initialized; servers that report no hierarchy delimiter for that mailbox; using a stale folder reference where DirectorySeparator was never populated.","solutions":["Ensure the parent folder's children were loaded (folder.Open + get subfolders) so DirectorySeparator is known before creating children.","Create the folder with its full hierarchical path from the top-level (root) folder instead of nesting: root.CreateFolder(\"Parent/Child\") with the correct separator.","Check folder.DirectorySeparator before attempting nested creation and fall back to flat names if '\\0'.","Some servers (or folder objects not opened as selectable) never expose a separator; verify server hierarchy support (LIST \"\" \"%%\")."],"exampleFix":"// before\nvar parent = client.GetFolder(\"Parent\");\nparent.CreateFolder(\"Child\"); // throws if separator unknown\n\n// after\nvar parent = client.GetFolder(\"Parent\");\nif (parent.DirectorySeparator == '\\0')\n    client.GetFolder(Cancellation).CreateFolder(\"Parent\" + sep + \"Child\");\nelse\n    parent.CreateFolder(\"Child\");","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(folder.FullName) || folder.DirectorySeparator != '\\0') { folder.CreateFolder(name); } else { /* create via full path from root */ }","typeGuard":null,"tryCatchPattern":"try { parent.CreateFolder(name); } catch (InvalidOperationException) { root.CreateFolder(parent.FullName + sep + name); }","preventionTips":["Check DirectorySeparator before creating children under a non-root folder.","Open/initialize the parent folder (children loaded) before child creation.","Prefer creating folders by full path from the root folder for predictable behavior.","Verify server hierarchy support with LIST \"\" \"%%\" when in doubt."],"tags":["imap","mailkit","folder-hierarchy","folder-creation"],"backgroundTag":"unsupported-operation","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}