{"record":{"id":"72ac63144688a5a8","repo":"AvaloniaUI/Avalonia","slug":"can-not-create-name-because-a-file-with-the-sa","errorCode":null,"errorMessage":"Can not create '{name}' because a file with the same name already exists.","messagePattern":"Can not create '(.+?)' because a file with the same name already exists\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Android/Avalonia.Android/Platform/Storage/AndroidStorageItem.cs","lineNumber":204,"sourceCode":"        }\n\n        return new AndroidStorageFile(Activity, newFile, this);\n    }\n\n    public async Task<IStorageFolder?> CreateFolderAsync(string name)\n    {\n        // Try to return an existing folder to avoid creating folder (1).\n        var existingItem = await GetItemAsync(name, true);\n        if (existingItem != null)\n        {\n            if (existingItem is IStorageFolder existingFolder)\n            {\n                return existingFolder;\n            }\n            else if (existingItem is IStorageFile)\n            {\n                // There is an item with the same name but it's not a folder. We can't create a folder in this case.\n                throw new IOException($\"Can not create '{name}' because a file with the same name already exists.\");\n            }\n        }\n        // Create new one and return it.\n        var treeUri = GetTreeUri().treeUri;\n        var newFolder = CreateDocument(Activity.ContentResolver!, treeUri!, Document.MimeTypeDir, name);\n        if (newFolder == null)\n        {\n            return null;\n        }\n\n        return new AndroidStorageFolder(Activity, newFolder, false, this, PermissionRoot);\n    }\n\n    public override async Task DeleteAsync()\n    {\n        if (!await EnsureExternalFilesPermission(false))\n        {\n            return;","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Android/Avalonia.Android/Platform/Storage/AndroidStorageItem.cs#L186-L222","documentation":"Thrown by AndroidStorageItem when CreateFolderAsync (the folder-creation path) finds an existing item with the requested name that is an IStorageFile rather than a folder. The Storage Access Framework cannot create a directory where a file already exists, so Avalonia throws IOException to reject the operation.","triggerScenarios":"Calling CreateFolderAsync(name) when a file (IStorageFile) with the identical name already exists under the target tree URI. The code first calls GetItemAsync(name, true); if it resolves to a file, the exception is thrown.","commonSituations":"Folder-name collisions with existing files; import/export flows that assume a name is free; bookmarked tree URIs whose contents changed between sessions; UIs that auto-generate folder names without collision checks.","solutions":["Call GetItemAsync(name) first; if the result is an IStorageFile, choose a different name or surface a conflict to the user.","Wrap CreateFolderAsync in a try/catch for IOException and offer a rename/retry dialog.","Validate generated names against the current listing before creation."],"exampleFix":"// before\nvar sub = await folder.CreateFolderAsync(name);\n\n// after\nvar existing = await folder.GetItemAsync(name);\nif (existing is IStorageFile)\n    throw new InvalidOperationException($\"A file named '{name}' already exists.\");\nvar sub = await folder.CreateFolderAsync(name);","handlingStrategy":"validation","validationCode":"// check for an existing file with the same name before creating the folder\nvar existing = await folder.GetItemAsync(name);\nif (existing is IStorageFile)\n    throw new IOException($\"A file named '{name}' already exists.\");\nvar sub = await folder.CreateFolderAsync(name);","typeGuard":"static bool NameIsFreeForFolder(IStorageFolder f, string name) => f.GetItemAsync(name).GetAwaiter().GetResult() is not IStorageFile;","tryCatchPattern":"try { return await folder.CreateFolderAsync(name); }\ncatch (IOException ex) when (ex.Message.Contains(\"file with the same name\"))\n{ /* prompt rename or pick new name */ throw; }","preventionTips":["Pre-check GetItemAsync(name) for file collisions before creating a folder.","Validate generated folder names against the current listing.","Catch IOException and offer a rename/retry dialog.","Refresh the listing before creation to avoid stale-collision surprises."],"tags":["android","storage","io","name-conflict","saf"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}