Question Details

No question body available.

Tags

c# .net-core

Answers (2)

January 12, 2026 Score: 3 Rep: 30,356 Quality: Medium Completeness: 80%

The answer to this question can be found in this article.

In a nutshell, when you manipulate a file through an API, the leading and trailing spaces in the file name will be retained as they are.

The Win32 API (CreateFile, FindFirstFile, etc.) uses a direct method to enumerate the files and folders on a local or remote file system. All files and folders are discoverable regardless of the inclusion or location of whitespace characters.

But for some operations on files in the File Explorer, the leading and trailing spaces in the file name will be removed before passing it to the API, the result, of course, is file-not-found.

File and Folder names that begin or end with the ASCII Space (0x20) will be saved without these characters.

As an aside, some methods in .NET, such as Path.GetFullPath, also remove trailing spaces, which leads to inconsistent behavior to other .NET methods, so it is best not to add leading or trailing spaces to file names.

January 12, 2026 Score: 1 Rep: 2,087 Quality: Low Completeness: 50%

Directories and files with names containing spaces are easily created and deleted not only programmatically:

var path = @"C:\code\test\net10.0\";
Directory.CreateDirectory(Path.Combine(path, @" Bad"));
Directory.CreateDirectory(Path.Combine(path, @" Bad \"));
Directory.CreateDirectory(Path.Combine(path, @" Bad  \"));

Console.WriteLine(string.Join(Environment.NewLine, Directory.GetDirectories(path).Select(d => $"\"{d}\"")));

Directory.Delete(Path.Combine(path, @" Bad")); Directory.Delete(Path.Combine(path, @" Bad \")); Directory.Delete(Path.Combine(path, @" Bad \"));

But also using the command line:

C:\code\test\net10.0>mkdir " bad \" C:\code\test\net10.0>rmdir " bad \"