title: String List of filepaths to structured object
body: <p>I'm currently creating an MVC page that will scan a folder and its children for all pdf files and display them. Allowing the user to click on a link and open the document.</p>
  
  <p>I have the following list of paths in List format.</p>
  
  <pre><code>Docs\\Work\\Test\\TestFile0.pdf
  Docs\\Work\\Test\\TEstFile1.pdf
  Docs\\Work\\Job\\JobFile0.pdf
  Docs\\Work\\Job\\JobFile1.pdf
  Docs\\School\\English\\Eng0.pdf
  Docs\\School\\English\\Eng1.pdf
  Docs\\School\\German\\Ger0.pdf
  Docs\\School\\German\\Ger0.pdf
  Docs\\Games\\Game0.pdf
  Docs\\Games\\Game1.pdf
  Docs\\Games\\Game2.pdf
  </code></pre>
  
  <p>I want to create a tree structure out of these that I can send to my View. Here is my model currently</p>
  
  <pre><code>public class FolderModel
  {
      public string Name { get; set; }
      public List&lt;FolderModel&gt; ChildFolders { get; set; }
      public List&lt;FileModel&gt; ChildFiles { get; set; }
  
      public FolderModel()
      {
          ChildFolders = new List&lt;FolderModel&gt;();
          ChildFiles = new List&lt;FileModel&gt;();
      }
  }
  
  public class FileModel
  {
      public string Name { get; set; }
      public string Path { get; set; }
  }
  </code></pre>
  
  <p>I've tried a number of different ways of doing this but I cannot seem to get it working properly. My current approach is to take each string, split it
  into a list of strings and use recursion to create folders and their children.</p>
  
  <pre><code>private FolderModel Create(List&lt;string&gt; path, FolderModel model, string origPath)
  {
      if (!path.Any())
      {
          return model;
      }
      var firstItem = path.First();
      if (firstItem.Contains("."))
      {
          //its a file
          var file = new FileModel()
          {
              Name = firstItem,
              Path = origPath
          };
          model.ChildFiles.Add(file);
          path.RemoveAt(0);
          model = Create(path, model, origPath);
          return model;
      }
      else
      {
          //its a folder
          var folder = new FolderModel()
          {
              Name = firstItem
          };
  
          if (!model.ChildFolders.Contains(folder))
          {
              model.ChildFolders.Add(folder);
          }
          path.RemoveAt(0);
          model = Create(path, folder, origPath);
          return model;
      }
  }
  </code></pre>
  
  <p>However I cannot seem to get it working properly and have tried a lot of different methods of doing it. Can anyone lend a hand or point me to something that will help me out.</p>