files-community/Files · error · IOException

Directory already exists.

Error message

Directory already exists.

What it means

Thrown by FtpStorageFolder.CreateFolderAsync when the target directory already exists. As with error 1, the guard is INVERTED: `if (overwrite && await ftpClient.DirectoryExists(...))` throws when overwrite is true and the directory exists - the opposite of overwrite semantics. With the default overwrite=false the existence is not caught here and flows into CreateDirectory.

Source

Thrown at src/Files.App.Storage/Ftp/FtpStorageFolder.cs:164

				// Throw exception since flag CreationCollisionOption.GenerateUniqueName was not satisfied
				throw new IOException("Couldn't generate unique name. File skipped.");
			}
			else
			{
				// File creation failed
				throw new IOException("File creation failed.");
			}
		}

		/// <inheritdoc/>
		public async Task<IChildFolder> CreateFolderAsync(string desiredName, bool overwrite = default, CancellationToken cancellationToken = default)
		{
			using var ftpClient = GetFtpClient();
			await ftpClient.EnsureConnectedAsync(cancellationToken);

			var newPath = $"{Id}/{desiredName}";
			if (overwrite && await ftpClient.DirectoryExists(newPath, cancellationToken))
				throw new IOException("Directory already exists.");

			var isSuccessful = await ftpClient.CreateDirectory(newPath, overwrite, cancellationToken);
			if (!isSuccessful)
				throw new IOException("Directory was not successfully created.");

			return new FtpStorageFolder(newPath, desiredName, this);
		}
	}
}

View on GitHub (pinned to 68c68a58d4)

Solutions

  1. Fix the inverted condition in FtpStorageFolder.cs:163 to `if (!overwrite && await ftpClient.DirectoryExists(newPath, cancellationToken))`.
  2. Pre-check existence and skip or rename when overwrite is not desired.
  3. Catch the IOException and treat an existing-directory result as non-fatal if idempotent creation is acceptable.

Example fix

// before (FtpStorageFolder.cs:163)
if (overwrite && await ftpClient.DirectoryExists(newPath, cancellationToken))
    throw new IOException("Directory already exists.");

// after
if (!overwrite && await ftpClient.DirectoryExists(newPath, cancellationToken))
    throw new IOException("Directory already exists.");
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check directory existence before creating.
var target = $"{ftpFolder.Id}/{desiredName}";
if (await ftpClient.DirectoryExists(target, ct))
{
    if (!overwrite) throw new IOException($"Directory {desiredName} already exists.");
}

Try / catch

try { await folder.CreateFolderAsync(name, overwrite, ct); }
catch (IOException ex) when (ex.Message == "Directory already exists.")
{ /* skip or rename, depending on desired behavior */ }

Prevention

When it happens

Trigger: Calling CreateFolderAsync(name, overwrite: true) on an FtpStorageFolder when a directory named {Id}/{desiredName} already exists on the server.

Common situations: Re-running a folder-creation step that previously partially succeeded; duplicate directory names in a batch import; automation passing overwrite=true expecting replace semantics.

Related errors


AI-assisted analysis of files-community/Files@68c68a58d4 (2026-08-13). Data as JSON: /api/errors/d288d1b5c68a2634. Report an issue: GitHub.