2013-10-11 92 views
4

我正在寻找一组简单的API方法,用于查找文件夹是否是另一个文件夹的子目录,以及它们之间有多少步骤。喜欢的东西:是否有C#方法从参考路径上下文件夹的数量?

int numberOfFoldersDown(string parentFolder, string subfolder) { ... } 

似乎相当有用的,虽然繁琐写,所以我想它应该是某处System.IO.PathSystem.IO.Directory组件,但我无法找到任何有用的方法存在。这些功能是否可用?或者我应该自己写吗?

+5

我*非常确信*您必须编写你自己的。 –

+0

很好的答案,虽然不是我所希望的。 ;-)有关该方法的任何建议? – Yellow

+0

这种功能的用途(似乎必须与'String.IndexOf'相同,但是与文件夹相同)是有问题的。通常保存路径(所以应用程序直接进入文件夹)或迭代(所以你不知道名字)。这就是它不存在的原因。一个附注(看答案),每当你要写这个函数时:尽可能多地使用'Path','File'和'Directory'功能(例如,'Path.GetFileName')。否则,你将面临很多问题:联电,结束反斜杠,不标准名称等。 – Sinatr

回答

1

没有内置AFAIK。

下面是一个使用两个PathDirectory方法的一些递归的例子:

internal class Program 
{ 
    private static void Main(string[] args) 
    { 
     Console.WriteLine(NumberOfFoldersDown(@"c:\temp\", @"c:\temp\"));     // 0 
     Console.WriteLine(NumberOfFoldersDown(@"c:\temp\", @"c:\temp\zz\"));    // 1 
     Console.WriteLine(NumberOfFoldersDown(@"c:\temp2\", @"c:\temp\zz\"));    // -1 
     Console.WriteLine(NumberOfFoldersDown(@"c:\temp2\", @"c:\temp2\zz\hui\55\"));  // 3 
     Console.WriteLine(NumberOfFoldersDown(@"c:\temp2\zz\", @"c:\temp2\zz\hui\55\")); // 2 

     Console.Read(); 
    } 

    public static int NumberOfFoldersDown(string parentFolder, string subfolder) 
    { 
     int depth = 0; 
     WalkTree(parentFolder, subfolder, ref depth); 
     return depth; 
    } 

    public static void WalkTree(string parentFolder, string subfolder, ref int depth) 
    { 
     var parent = Directory.GetParent(subfolder); 
     if (parent == null) 
     { 
      // Root directory and no match yet 
      depth = -1; 
     } 
     else if (0 != string.Compare(Path.GetFullPath(parentFolder).TrimEnd('\\'), Path.GetFullPath(parent.FullName).TrimEnd('\\'), true)) 
     { 
      // No match yet, continue recursion 
      depth++; 
      WalkTree(parentFolder, parent.FullName, ref depth); 
     } 
    } 
} 
0

你可以通过所有文件夹重复,获取路径名,一旦你得到一个路径名,遍历该字符串的每个“/”检查,然后指望有很多很多的/有,因此,例如:

C:/用户/ Program Files文件/

有3 /所以有3周跳的文件/文件夹你想

希望这是一些有用的信息,你想要什么。

+2

首先它是'\'不是'/',其次是不是真的可靠。由于UNC路径(\\ share \ bla - > 3 Folders),以及如果程序在文件夹后添加了反斜杠(例如C:\ Test \ - > 2 Folders) –

+0

'(\\ share \ bla'您总是可以将双斜杠作为1计数,并且如果您不想计算文件后面的最后一个反斜杠,则始终可以忽略该值,无论如何,这是关于它如何工作的一个粗略的想法,从来没有试图做这样的事情,所以只是给我一个方法,我怎么试试它 – 2013-10-11 12:47:57

+0

这是一个很好的开始,但斜杠是一个问题。确实会出现正斜杠,但另一个可能出现的问题是这样的情况:'folder /../ otherfolder'。因此,如上所述,这一切都变得非常乏味,因此我希望找到一个现有的函数来做到这一点。 – Yellow

0

只要我知道只有这样,才能做到这一点是

parent folder : Split the path on "\" and think to remove the drive letter and colon 
subfolder  : navigate it recursively (which can be time consuming) 

许多其他的点都在这个被视为但这是大创意!

编辑: 确定这里是你需要什么的......样品,更多的工作要做,但它是一个良好的开端,我认为...... 的好处是,你未知的文件夹结构工作,不只是一个代表路径的字符串。

public static class FoldersHelper 
{ 
    public static int ParentFolderCount(string path) 
    { 
     int parentcnt = 0; 

     if (System.IO.Directory.Exists(path)) 
     { 
      string pathroot = Path.GetPathRoot(path); 
      path = path.Remove(1, pathroot.Length); 
      parentcnt = path.Split('\\').Count()-1; 
      return parentcnt; 
     } 
     else 
     { 
      throw new Exception("not a folder exception"); 
     } 

     return 0; 
    } 

    public static int ChildFolderCount(string path) 
    { 
     int childcnt = 0; 
     int maxchild = 0; 

     if (System.IO.Directory.Exists(path)) 
     { 
      if (Directory.GetDirectories(path).Length > 0) 
      { 
       foreach (string subpath in Directory.GetDirectories(path)) 
       { 
        childcnt = ChildFolderCount(subpath) + 1; 
        if (childcnt > maxchild) maxchild = childcnt; 
       } 
      } 
     } 
     else 
     { 
      throw new Exception("not a folder exception"); 
     } 

     return maxchild; 
    } 

} 
0

以某种方式查找路径,也许通过遍历它们。

当您发现一个拆分反斜杠上的路径时,将这些部分放入一个数组中。反转数组,遍历该数组检查文件夹名是否与父文件夹名字符串匹配。如果是这样,返回索引+ 1的步骤?