While writing some property-based tests in F#, I have discovered a very strange behavior of the constructor of System.IO.DirectoryInfo
. Based on the documentation, I would expect an ArgumentException
to be thrown any time I try to construct a DirectoryInfo
with an invalid path character; and I would expect Path.GetInvalidPathChars()
to be a reliable source for these. Its documentation says, "The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names"; but it says nothing about the method returning characters that are not invalid! Form feed ('\012'
) is one of these invalid characters, as you might expect; I would expect any kind of control character to be invalid in a path name. And indeed, if I try to construct a DirectoryInfo
with one in the middle or the beginning, it doesn't work. However, it does work if I put it at the end:
The absolute path produced does not have the form feed in it though, confirming that this is indeed and invalid path character. I confirmed that the directory I created in that REPL session was there, with no control characters in the name. However, to make it even odder, when I convert the DirectoryInfo back to a string, the garbage character is maintained:
Then same behavior is exhibited for horizontal tab '\009'
, line feed '\010'
, and vertical tab '\011'
, and carriage return '\013'
, but all other control characters are rejected. One would think they would at least be trimmed if they were going to be accepted.
What's going on here? Is this a bug in DirectoryInfo
, or is there some rational/consistent explanation of this behavior in .NET? (And what other such potholes might I have to look for!)