2013-10-11 25 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的步驟?