How can I check whether a path to a file that doesn’t necessarily exists points to a location inside a particular directory? Say I have a method:
bool IsInside(string path, string folder)
{
//...
}
Then, if I call it like:
IsInside("C:\\Users\\Dude\\Hi", "C:\\Users\\Dude\\Hi\\SubFolder\\SubSubFolder\\tile.txt")
should return true (note the sub folder), but if I call it like:
IsInside("C:\\Users\\Dude\\Hi", "C:\\Users\\Dude\\BadFolder\\SubFolder\\SubSubFolder\\tile.txt")
should return false. The only thing I can think of right now is using string’s StartsWith, but sounds kinda hacky to me. I haven’t found a native .NET method that would check this either.
You could try the
string.IndexOfmethod. If you use the overload with the StringComparison enumeration it should give you the result you need.From above link: