Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in Directory.GetParent?

I was hit in the face by a very weird behavior of the System.IO.Directory.GetParent method:

string path1 = @"C:\foo\bar";
DirectoryInfo parent1 = Directory.GetParent(path1);
Console.WriteLine (parent1.FullName); // Prints C:\foo, as expected

// Notice the extra backslash. It should still refer to the same location, right ?
string path2 = @"C:\foo\bar\";
DirectoryInfo parent2 = Directory.GetParent(path2);
Console.WriteLine (parent2.FullName); // Prints C:\foo\bar !!!

I would consider it a bug, but this method has been there since 1.0, so I guess it would have been detected by now. On the other hand, if it's as designed, I can't think of a sensible explanation for such a design...

What do you think ? Is it a bug ? If not, how do you explain this behavior ?

like image 231
Thomas Levesque Avatar asked Oct 18 '10 22:10

Thomas Levesque


3 Answers

Some googling reveals some thoughts:

DirectoryInfo di = new DirectoryInfo(@"C:\parent\child");
Console.WriteLine(di.Parent.FullName);

and

DirectoryInfo di = new DirectoryInfo(@"C:\parent\child\");
Console.WriteLine(di.Parent.FullName);

Both return "C:\parent"

I can only assume Directory.GetParent(...) can't assume that C:\parent\child is a directory instead of a file with no file extension. DirectoryInfo can, because you're constructing the object that way.


Personally what I think is that when there is a backslash, the string is treated as a path to the "null file" inside the directory (that is, a file with no name and extension). Apparently, those can exist (there should be a link, but for some reason I can't find anything).

Try constructing a FileInfo object out of path2. You'll see it's properly constructed, has String.Empty as its name and extension, does not exist and has C:\foo\bar as its DirectoryName. Given that, the situation makes sense: the parent object for this "null file" is indeed C:\foo\bar.

like image 153
GSerg Avatar answered Nov 17 '22 23:11

GSerg


I agree with GSerg. Just to add some additional firepower, I'm going to add the following code snippets obtained with Reflector.

The Directory.GetParent function basically just calls the Path.GetDirectoryName function:

[SecuritySafeCritical]
public static DirectoryInfo GetParent(string path)
{
    if (path == null)
    {
        throw new ArgumentNullException("path");
    }
    if (path.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_PathEmpty"), "path");
    }
    string directoryName = Path.GetDirectoryName(Path.GetFullPathInternal(path));
    if (directoryName == null)
    {
        return null;
    }
    return new DirectoryInfo(directoryName);
}

The Parent property of the DirectoryInfo basically strips off a trailing slash and then calls Path.GetDirectoryName:

public DirectoryInfo Parent
{
    [SecuritySafeCritical]
    get
    {
        string fullPath = base.FullPath;
        if ((fullPath.Length > 3) && fullPath.EndsWith(Path.DirectorySeparatorChar))
        {
            fullPath = base.FullPath.Substring(0, base.FullPath.Length - 1);
        }
        string directoryName = Path.GetDirectoryName(fullPath);
        if (directoryName == null)
        {
            return null;
        }
        DirectoryInfo info = new DirectoryInfo(directoryName, false);
        new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read, info.demandDir, false, false).Demand();
        return info;
    }
}
like image 23
Russell McClure Avatar answered Nov 17 '22 23:11

Russell McClure


This is pretty interesting. First when I read this I was pretty sure this would be a bug, but when I thought a little bit longer about it I came to the conclusion that probably the intent is that path should not be a directory but a full or relative path to a file. So

c:\somenonexistingpath\to\a\directory\

gets interpreted as a path to a file with no name in ...\directory. That's kind of silly, but if we assume that the programmers at Microsoft were expecting a full path to a file, it makes sense not to cover this case.

EDIT:

Note that

c:\dir\makefile -> c:\dir

c:\dir\build.msbuild -> c:\dir

give the parent as expected.

like image 33
steinar Avatar answered Nov 18 '22 00:11

steinar