DirectoryInfo.CreateSubdirectory(path) throws ArgumentException for newline and tab nonsignificant trailing characters
There's a discrepancy in the way DirectoryInfo.CreateSubdirectory and Directory.CreateDirectory handle trailing tab and newline characters on Windows. The below code exhibits the difference:
{ // Directory.CreateDirectory curtails trailing tab and newline characters
string filePath = IOServices.RemoveTrailingSlash(GetTestFilePath());
DirectoryInfo result = Directory.CreateDirectory(filePath + "\n");
Assert.Equal(filePath, IOServices.RemoveTrailingSlash(result.FullName));
}
{ // DirectoryInfo.CreateSubdirectory throws an ArgumentException when there are newline or tab characters anywhere in a file name, even as trailing chars
string fileName = IOServices.RemoveTrailingSlash(GetTestFileName());
DirectoryInfo parentDir = new DirectoryInfo(TestDirectory);
Assert.Throws<ArgumentException>(() => parentDir.CreateSubdirectory(fileName + "\n")); //illegal characters in path
}
This is somewhat related to dotnet/runtime#14868 in that DirectoryInfo.CreateDirectory handles whitespace very differently than Directory.CreateDirectory or even DirectoryInfo.Create.
Because this behavior is present on the desktop framework already, it has been decided that it will stay this way. From this perspective, directory.CreateSubdirectory(dirName); should be treated as being equivalent to Directory.CreateDirectory(Path.Combine(directory.FullPath, dirName)); with regards to error checking.