2014-01-09 132 views
5

我坐在一個腦筋急轉彎,我似乎無法完成。我正在嘗試創建特定的文件夾結構。結構解釋如下:遞歸文件夾創建

在指定的根文件夾中,應用程序應創建10個文件夾,'0' - '10'。在這些內部,再次應該是文件夾'0' - '10'等。這必須繼續到用戶定義的級別。

使用for循環,我已經成功地得到這個,到目前爲止,但可以想像,一個遞歸函數會顯得少了很多凌亂,但在同一時間融化了我的大腦試圖弄明白d:

static void Main(string[] args) 
     { 
      string basePath = Path.Combine(Environment.CurrentDirectory, "Lib"); 

      for (int a = 0; a < 10; a++) 
      { 
       CreateFolders(basePath); 
       basePath = Path.Combine(basePath, a.ToString()); 

       for (int b = 0; b < 10; b++) 
       { 
        CreateFolders(basePath); 
        basePath = Path.Combine(basePath, b.ToString()); 

        for (int c = 0; c < 10; c++) 
        { 
         CreateFolders(basePath); 
         basePath = Path.Combine(basePath, c.ToString()); 

         for (int d = 0; d < 10; d++) 
         { 
          CreateFolders(basePath); 
          basePath = Path.Combine(basePath, d.ToString()); 

          basePath = Helpers.DirMoveBack(basePath); 
         } 
         basePath = Helpers.DirMoveBack(basePath); 
        } 
        basePath = Helpers.DirMoveBack(basePath); 
       } 
       basePath = Helpers.DirMoveBack(basePath); 
      } 

      Console.ReadLine(); 
     } 

// Creates folders '0' - '9' in the specified path 
static void CreateFolders(string path) 
    { 
     for (int a = 0; a < 10; a++) 
     { 
      Directory.CreateDirectory(string.Format("{0}\\{1}", path, a)); 
      Console.WriteLine(string.Format("{0}\\{1}", path, a)); 
     } 
    } 


public static class Helpers 
    { 

     // Moves the directory back one step 
     public static string DirMoveBack(string path) 
     { 
      for (int a = path.Length - 1; a > 0; a--) 
       if (path[a] == '\\') 
        return path.Substring(0, a); 

      return path; 
     } 
    } 

正如你所看到的,這很雜亂。如果你運行代碼,它會創建所需的文件夾結構,但我希望它遞歸地完成。我試圖擴展我的思維方式,而這似乎是一個真正的腦力激盪。任何幫助將不勝感激

+0

我可以讓它發生,但0-10使11個文件夾,而不是10:○ – evanmcdonnal

+0

的條件是<10,所以它僅創建第九屆文件夾,或者你說的是遞歸?我知道迭代可能是理想的,但我似乎無法使用遞歸來工作。是的,你將不得不從0 - > 0 - > 0 - >(0-> 9)創建文件夾,第一步, (0 - > 9),回到第一步,直到你回到根目錄中的第9個文件夾。我只是好奇,它將如何工作:) –

回答

5

是的,遞歸較短:)天作之合任何類似於一個樹狀結構:

static void Main(string[] args) { 
     CreateFolders(3, "c:\\temp\\temp"); 
    } 

    static void CreateFolders(int depth, string path) { 
     if (depth <= 0) return; 
     for (int ix = 0; ix <= 10; ++ix) { 
      var dir = Path.Combine(path, ix.ToString()); 
      System.IO.Directory.CreateDirectory(dir); 
      CreateFolders(depth - 1, dir); 
     } 
    } 
+0

非常好的答案.. :) – Nico

+0

啊,這正是我想要的。這很棒,非常感謝快速輸入:D –

4

這是一個非常簡單的系統,用於x創建0-10文件夾遞歸水平。導致圖片中列出的結構。這個概念非常簡單,該方法調用自己傳遞參數pathdepth。如果path是根路徑,則應在該遞歸級別創建文件夾,depth是要創建的其餘子文件夾。你會注意到每次遞歸調用depth參數減1,直到它等於零。在零遞歸停止。

static void Main(string[] args) 
{ 
    int maxDepth = 5; 
    string initialPath = @"D:\testFolders"; 
    createFolders(initialPath, maxDepth); 
} 

static void createFolders(string path, int depth) 
{ 
    depth--; 
    for (int i = 0; i <= 10; i++) 
    { 
     string directory = Path.Combine(path, i.ToString()); 
     if (!Directory.Exists(directory)) 
      Directory.CreateDirectory(directory); 
     if (depth > 0) 
      createFolders(directory, depth); 
    } 
} 

enter image description here

相關問題