user626528 user626528 - 1 year ago 60
C# Question

How to check if one path is a child of another path?

How to check if one path is a child of another path?

Just checking for substring is not a way to go, because there can items such as . and .., etc

Answer Source

This would be one way to go, you have path A and B, convert them to full paths with the Path.GetFullPath() function. Next check if one of the full paths is a starting substring of the other.

So that would be

if (Path.GetFullPath(A).StartsWith(Path.GetFullPath(B)) ||
    Path.GetFullPath(B).StartsWith(Path.GetFullPath(A)))
   { /* ... do your magic ... */ }