Directory.GetParent and slashes

Running this snippet

var path1 = @"C:\Temp\SomeFolder";
var path2 = @"C:\Temp\SomeFolder\";
Console.WriteLine(Directory.GetParent(path1));
Console.WriteLine(Directory.GetParent(path2));

Output

C:\Temp

C:\Temp\SomeFolder

Same story with Path.GetDirectoryName(), which also can be used to obtain parent folder, but will similarly fail in case of \ at the end.

I do really like Path.Combine() for ignoring slashes at the end and do really hate some guys who make some methods returning path with slash (referring to AppDomain.CurrentDomain.BaseDirectory *angryface*).

Question: how to properly handle possible slash at the end of given path when I need to get one of the parent directories?


Here is another snippet

var path1 = @"C:\Temp\SomeFolder";
var path2 = @"C:\Temp\SomeFolder\";
var dir1 = @"Test";
var dir2 = @"Test\";
Console.WriteLine(Path.Combine(path1, dir1));
Console.WriteLine(Path.Combine(path1, dir2));
Console.WriteLine(Path.Combine(path2, dir1));
Console.WriteLine(Path.Combine(path2, dir2));

Output

C:\Temp\SomeFolder\Test

C:\Temp\SomeFolder\Test\

C:\Temp\SomeFolder\Test

C:\Temp\SomeFolder\Test\

Path.Combine works properly (ignoring ending slash of the first path, or well, adding it when it's missing), while persisting ending slash of second path (for whatever reasons, I do not care, because using resulting path in more Path.Combines will works as well).

As I understand Test and Test\ pointing at the same folder in directory structure. And if I want to get parent (previous folder), then Directory.GetParent should return me previous folder, and not Test folder again.

Answers


Question: how to properly handle possible slash at the end of given path when I need get one of the parent directories?

Just trim it from the input string. Use TrimEnd:

Console.WriteLine(Directory.GetParent(path1.TrimEnd('\\')));

However returning the complete path when the string is terminated by \ is desired behaviour

Directory.GetParent Method

However, passing "C:\Directory\SubDirectory\" returns "C:\Directory\SubDirectory", because the ending directory separator is after "SubDirectory".


Some Path theory:

A file system contains Containers (Folders, Directories) and Elements (Files).

A "Path" Contains a set of (hierarchical) Containers and probably an "Element" as last element, all separated by slashes.

In theory, if the "Element" is missing, there is a trailing slash to indicate that the last item is a Container:

/Directory/Subdirectory/

And a Path without trailing slash points to an "Element", not an "Container"

/Directory/Subdirectory/Element

This is the theoretically clean way to handle things. Because it is often too cumbersome for end-users to deal with clean theory, the software often tolerates a missing trailing slash after a final "Container":

/Directory/Subdirectory

This is just a "hack" to make the life of the end-user easier and basically agiuants the theoretically sound specs for pathes.


Edit

This convention has its drawbacks and introduces missbehaviour at different places.

Look at the Directory.GetParent() method. It accepts a string and works with string manipulations only. So this method can not decide if the last item is a "Container" or an "Element" if the trailing slash is missing. It assumes to see an "Element" as last element and returns the next "Container". If the last item happens to be a "Container" (and we mean "all content in this container") this is indeed the wrong answer.


Need Your Help

Substr based on bytes rather than character count

javascript byte

I'm creating an input system where a fields maximum value can only be 200 bytes. I am counting the remaining number of bytes by using the following (this method might but up for debate, too!):

How to parse diffgram anytype response from .net webservice

java android

I am getting the following response from a .net webservice which is of anytype format.I need to parse this format and read the data value like RemMessage and INVM_ID provided in the format.I am not...