AvaloniaUI/Avalonia · error · IOException

Can not create '{name}' because a directory with the same na

Error message

Can not create '{name}' because a directory with the same name already exists.

What it means

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.

Source

Thrown at src/Android/Avalonia.Android/Platform/Storage/AndroidStorageItem.cs:176

    {
    }

    public async Task<IStorageFile?> CreateFileAsync(string name)
    {
        // Try to return an existing file to avoid creating file (1).
        var existingItem = await GetItemAsync(name, false);
        if (existingItem != null)
        {
            if (existingItem is IStorageFile existingFile)
            {
                // The file should be truncated when it is created.
                using (var _ = await existingFile.OpenWriteAsync()) { }
                return existingFile;
            }
            else if (existingItem is IStorageFolder)
            {
                // There is an item with the same name but it's not a file. We can't create a file in this case.
                throw new IOException($"Can not create '{name}' because a directory with the same name already exists.");
            }
        }
        // Create new one and return it.
        var treeUri = GetTreeUri().treeUri;
        var mimeType = MimeTypeMap.Singleton?.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl(name)) ?? "application/octet-stream";
        var newFile = DocumentsContract.CreateDocument(Activity.ContentResolver!, treeUri!, mimeType, name);
        if(newFile == null)
        {
            return null;
        }

        return new AndroidStorageFile(Activity, newFile, this);
    }

    public async Task<IStorageFolder?> CreateFolderAsync(string name)
    {
        // Try to return an existing folder to avoid creating folder (1).
        var existingItem = await GetItemAsync(name, true);

View on GitHub (pinned to 11c5427268)

Solutions

  1. Before creating, call GetItemAsync(name) and check whether a folder with that name exists; if so, pick a different name or prompt the user.
  2. Catch IOException around CreateFileAsync and surface a user-facing 'name conflict' message with a retry/rename option.
  3. Sanitize/validate user input for forbidden or duplicate names before invoking the storage API.

Example fix

// before
var file = await folder.CreateFileAsync(name);

// after
var existing = await folder.GetItemAsync(name);
if (existing is IStorageFolder)
    throw new InvalidOperationException($"A folder named '{name}' already exists.");
var file = await folder.CreateFileAsync(name);
Defensive patterns

Strategy: validation

Validate before calling

// check for an existing folder with the same name before creating the file
var existing = await folder.GetItemAsync(name);
if (existing is IStorageFolder)
    throw new IOException($"A folder named '{name}' already exists.");
var file = await folder.CreateFileAsync(name);

Type guard

static bool NameIsFreeForFile(IStorageFolder f, string name) => f.GetItemAsync(name).GetAwaiter().GetResult() is not IStorageFolder;

Try / catch

try { return await folder.CreateFileAsync(name); }
catch (IOException ex) when (ex.Message.Contains("directory with the same name"))
{ /* prompt rename or pick new name */ throw; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/692ccdfa2bc87390. Report an issue: GitHub.