I\'m working on something that requires traversing through the file system and for any given path, I need to know how \'deep\' I am in the folder structure. Here\'s what I\'m cu
If the directory has a backslash at the end, you get a different answer than when it doesn't. Here's a robust solution to the problem.
string pathString = "C:\\temp\\"
var rootFolderDepth = pathString.Split(Path.DirectorySeparatorChar).Where(i => i.Length > 0).Count();
This returns a path length of 2. If you do it without the where statement, you get a path length of 3 or a path length of 2 if you omit the last separator character.