runtime: DirectoryInfo.CreateSubdirectory fails when DirectoryInfo path is a root directory
Description
The same issue as #26448
Reproduction Steps
static void Main(string[] args)
{
var d = new DirectoryInfo(@"C:\");
d.CreateSubdirectory("bar");
}
Expected behavior
It creates the subdirectory
Actual behavior
System.ArgumentException: The directory specified, 'bar', is not a subdirectory of 'C:\'.
Parameter name: path
at System.IO.DirectoryInfo.CreateSubdirectory(String path)
...
Regression?
In .NET Framework 4.8 it works fine
Known Workarounds
No response
Configuration
<TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
<OutputType>Library</OutputType>
<Platforms>x86</Platforms>
Win 11 Pro 21H2 22631.2861 x64
Other information
This doesn’t throw:
static void Main(string[] args)
{
var d = new DirectoryInfo(@"C:\foo");
d.CreateSubdirectory("bar");
}
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 1
- Comments: 15 (7 by maintainers)
It seems the origin of the issue is the
PathInternal.IsDirectorySeparator(newPath[trimmedCurrentPath.Length])expression inhttps://github.com/dotnet/runtime/blob/d960be890a25df350184089056e87e2bfc1446c5/src/libraries/System.Private.CoreLib/src/System/IO/DirectoryInfo.cs#L85-L98
For DirectoryInfo’s representing root directories like C:\,
Path.TrimEndingDirectorySeparatordoes not remove the separator from its full path, withtrimmedCurrentPaththen still containing the trailing separator of the root dir path.Hence, in this situation using
trimmedCurrentPath.Lengthas index intonewPathpoints not at the separator but at the character after the separator, thus theifcondition incorrectly fails and the subsequent ArgumentException is being thrown.Side note: The current
CreateSubdirectoryimplementation has further issues. For example, it likes to throw an exception when invoked likeCreateSubdirectory(@"\bar")with an entirely misleading message of “Second path fragment must not be a drive or UNC name.”The root cause seems to be the DirectoryInfo being a root directory.
new DirectoryInfo(@"C:\")represents a root directory – issue triggered.new DirectoryInfo(@"C:\foo\")does not represents a root directory – issue not triggered.new DirectoryInfo(@"C:\foo\..")represents a root directory – issue triggered.Side note:
Be extremely careful with paths consisting only of a drive letters without a [back]slash. A path consisting only of a drive letter like
C:does NOT specify the root directory of drive C:. It rather specifies the working directory of drive C:.As far as .NET is concerned, if drive X is the drive of the
Directory.GetCurrentDirectory()path, then the path@"X:"refers to thisDirectory.GetCurrentDirectory()path. If drive X is not the drive of theDirectory.GetCurrentDirectory()path, then@"X:"coincides with the root directory of drive X. (Note that other runtime environments or shells might keep track of last working directory of each drive individually, so the key thing to remember here is that a path consisting only of a drive letter without [back]slash is not referring to the root directory, but to a working directory, whatever that working directory might be…)