C# Best way to get folder depth for a given path?

后端 未结 7 2173
野趣味
野趣味 2021-02-19 18:29

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

7条回答
  •  一个人的身影
    2021-02-19 19:11

    Assuming your path has already been vetted for being valid, in .NET 3.5 you could also use LINQ to do it in 1 line of code...

    Console.WriteLine(@"C:\Folder1\Folder2\Folder3\Folder4\MyFile.txt".Where(c => c = @"\").Count);

提交回复
热议问题