{"record":{"id":"692ccdfa2bc87390","repo":"AvaloniaUI/Avalonia","slug":"can-not-create-name-because-a-directory-with-t","errorCode":null,"errorMessage":"Can not create '{name}' because a directory with the same name already exists.","messagePattern":"Can not create '(.+?)' because a directory with the same name already exists\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Android/Avalonia.Android/Platform/Storage/AndroidStorageItem.cs","lineNumber":176,"sourceCode":"    {\n    }\n\n    public async Task<IStorageFile?> CreateFileAsync(string name)\n    {\n        // Try to return an existing file to avoid creating file (1).\n        var existingItem = await GetItemAsync(name, false);\n        if (existingItem != null)\n        {\n            if (existingItem is IStorageFile existingFile)\n            {\n                // The file should be truncated when it is created.\n                using (var _ = await existingFile.OpenWriteAsync()) { }\n                return existingFile;\n            }\n            else if (existingItem is IStorageFolder)\n            {\n                // There is an item with the same name but it's not a file. We can't create a file in this case.\n                throw new IOException($\"Can not create '{name}' because a directory with the same name already exists.\");\n            }\n        }\n        // Create new one and return it.\n        var treeUri = GetTreeUri().treeUri;\n        var mimeType = MimeTypeMap.Singleton?.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl(name)) ?? \"application/octet-stream\";\n        var newFile = DocumentsContract.CreateDocument(Activity.ContentResolver!, treeUri!, mimeType, name);\n        if(newFile == null)\n        {\n            return null;\n        }\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);","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Android/Avalonia.Android/Platform/Storage/AndroidStorageItem.cs#L158-L194","documentation":"Thrown by AndroidStorageItem when CreateFileAsync (the file-creation path) finds an existing item with the requested name that is an IStorageFolder rather than a file. Android's Storage Access Framework cannot create a file where a directory already exists, so Avalonia surfaces this as an explicit IOException rather than silently failing or returning null.","triggerScenarios":"Calling CreateFileAsync(name) (or the underlying file-creation flow) when a folder with the identical name already exists at the target tree URI. The code first calls GetItemAsync(name, false); if it resolves to a folder, the exception is thrown.","commonSituations":"User-supplied or generated filenames that collide with existing directory names; file/folder creation UIs that don't check for name collisions; restoring from a bookmark where the tree state changed; mirroring a filesystem layout that mixes file and folder names.","solutions":["Before creating, call GetItemAsync(name) and check whether a folder with that name exists; if so, pick a different name or prompt the user.","Catch IOException around CreateFileAsync and surface a user-facing 'name conflict' message with a retry/rename option.","Sanitize/validate user input for forbidden or duplicate names before invoking the storage API."],"exampleFix":"// before\nvar file = await folder.CreateFileAsync(name);\n\n// after\nvar existing = await folder.GetItemAsync(name);\nif (existing is IStorageFolder)\n    throw new InvalidOperationException($\"A folder named '{name}' already exists.\");\nvar file = await folder.CreateFileAsync(name);","handlingStrategy":"validation","validationCode":"// check for an existing folder with the same name before creating the file\nvar existing = await folder.GetItemAsync(name);\nif (existing is IStorageFolder)\n    throw new IOException($\"A folder named '{name}' already exists.\");\nvar file = await folder.CreateFileAsync(name);","typeGuard":"static bool NameIsFreeForFile(IStorageFolder f, string name) => f.GetItemAsync(name).GetAwaiter().GetResult() is not IStorageFolder;","tryCatchPattern":"try { return await folder.CreateFileAsync(name); }\ncatch (IOException ex) when (ex.Message.Contains(\"directory with the same name\"))\n{ /* prompt rename or pick new name */ throw; }","preventionTips":["Pre-check GetItemAsync(name) for folder collisions before creating a file.","Sanitize/validate user-supplied filenames.","Catch IOException and offer a rename/retry UI.","Avoid generating names that mirror existing directory names."],"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"}