AvaloniaUI/Avalonia · error · IOException
Can not create '{name}' because a file with the same name al
Error message
Can not create '{name}' because a file with the same name already exists. What it means
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.
Source
Thrown at src/Android/Avalonia.Android/Platform/Storage/AndroidStorageItem.cs:204
}
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);
if (existingItem != null)
{
if (existingItem is IStorageFolder existingFolder)
{
return existingFolder;
}
else if (existingItem is IStorageFile)
{
// There is an item with the same name but it's not a folder. We can't create a folder in this case.
throw new IOException($"Can not create '{name}' because a file with the same name already exists.");
}
}
// Create new one and return it.
var treeUri = GetTreeUri().treeUri;
var newFolder = CreateDocument(Activity.ContentResolver!, treeUri!, Document.MimeTypeDir, name);
if (newFolder == null)
{
return null;
}
return new AndroidStorageFolder(Activity, newFolder, false, this, PermissionRoot);
}
public override async Task DeleteAsync()
{
if (!await EnsureExternalFilesPermission(false))
{
return;View on GitHub (pinned to 11c5427268)
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.
Example fix
// before
var sub = await folder.CreateFolderAsync(name);
// after
var existing = await folder.GetItemAsync(name);
if (existing is IStorageFile)
throw new InvalidOperationException($"A file named '{name}' already exists.");
var sub = await folder.CreateFolderAsync(name); Defensive patterns
Strategy: validation
Validate before calling
// check for an existing file with the same name before creating the folder
var existing = await folder.GetItemAsync(name);
if (existing is IStorageFile)
throw new IOException($"A file named '{name}' already exists.");
var sub = await folder.CreateFolderAsync(name); Type guard
static bool NameIsFreeForFolder(IStorageFolder f, string name) => f.GetItemAsync(name).GetAwaiter().GetResult() is not IStorageFile;
Try / catch
try { return await folder.CreateFolderAsync(name); }
catch (IOException ex) when (ex.Message.Contains("file with the same name"))
{ /* prompt rename or pick new name */ throw; } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Can not create '{name}' because a directory with the same na
- Failed to open content stream
- result.GetStringExtra("error")
- File path is expected to be an absolute link with "file" or
- Folder path is expected to be an absolute link with "file" o
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/72ac63144688a5a8.
Report an issue: GitHub.