title: Saving directory paths in JSON?
body: <p>I have an application that displays a treeview of a folders, with the format below:</p>
  
  <pre><code>- Main Folder
      - SubFolder
      - SubFolder
          - SubFolder
      - SubFolder
  </code></pre>
  
  <p>Questions:
  1) <code>How do I search for all the folders (not files) and add them to a List called syncDirectories</code><strong>(FIXED)</strong></p>
  
  <p>2) <code>How would I then iterate through my JSON object and save it back to List&lt;SavedData&gt; in order to add/remove data</code> <strong>(FIXED)</strong></p>
  
  <p>3) <code>How would I loop through my syncDirectories List and add all the nodes to the TreeView in WPF. (Keep in mind the treeview with checkbox class i am using.</code></p>
  
  <p>And so on. Also, this is an example to demonstrate the format, and users could have more subdirectories.</p>
  
  <p>Each of these folders has a checkbox on it thanks to this <a href="http://www.thebestcsharpprogrammerintheworld.com/blogs/treeview-with-checkbox-in-wpf-using-csharp.aspx" rel="nofollow">TREEVIEW CLASS.</a></p>
  
  <p>In order to save all the folders and subfolders the user adds (and checks), I thought saving a JSON file listing all the folder and subfolder paths, along with their (true/false) checked values would be a good idea. Right? I am using Json.NET.</p>
  
  <p>Messing around with some JSON, I came up with this format:</p>
  
  <pre><code>{   
      "path": "path/here",
      "subDirectories": [
          {"path": "sub/path/here","sync": false},
          {"path": "sub/path/here","sync": true},
          {"path": "sub/path/here","sync": false}
      ]
  }
  </code></pre>
  
  <p>Which translated into the class:</p>
  
  <pre><code>public class SavedData
  {
      public string path { get; set; }
      public List&lt;SubDirectory&gt; subDirectories { get; set; }
  }
  
  public class SubDirectory
  {
      public string path { get; set; }
      public bool sync { get; set; }
      public List&lt;SubDirectory&gt; subDirectories { get; set; }
  }
  </code></pre>
  
  <p>This raises the question, How do I search for all the folders (not files) and add them to a <code>List</code> called <code>syncDirectories</code>?</p>
  
  <p>Here is what I have so far in my <code>addDirectory</code> function:</p>
  
  <pre><code>private void addDirectory()
  {
      var dialog = new FolderBrowserDialog();
      DialogResult result = dialog.ShowDialog();
      if (result == System.Windows.Forms.DialogResult.OK)
      {
          SavedData data = new SavedData();
          data.path = dialog.SelectedPath;
          syncedDirectories.Add();
      }
      // Add the data to syncedDirectories List, then save the list and refresh the treeView
      saveData();
  }
  </code></pre>
  
  <p>And the save function:</p>
  
  <pre><code>    public void saveData()
      {
          JsonSerializer serializer = new JsonSerializer();
          using (StreamWriter sw = new StreamWriter(appData))
          using (JsonWriter writer = new JsonTextWriter(sw))
          {
              serializer.Serialize(writer, syncedDirectories);
          }
      }
  </code></pre>
  
  <p>When I save the data into a file, How would I then iterate through my JSON object and save it back to <code>List&lt;SavedData&gt;</code> in order to add/remove data?</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I have figured out how to handle my data (Below), but how exactly would i add these "nodes" to the treeview?</p>
  
  <pre><code>    public void getDirectories(string dir)
      {
          // Create the Data Object
          SavedData data = new SavedData();
          data.path = dir;
          data.subDirectories = new List&lt;SubDirectory&gt;();
          foreach (string directory in Directory.GetDirectories(dir))
          {
              SubDirectory subDir = new SubDirectory();
              subDir.path = directory;
              subDir.subDirectories = getSubDir(directory);
              data.subDirectories.Add(subDir);
          }
          syncedDirectories.Add(data);
      }
  
      private List&lt;SubDirectory&gt; getSubDir(string dir)
      {
          List&lt;SubDirectory&gt; dataList = new List&lt;SubDirectory&gt;();
          SubDirectory subDir = new SubDirectory();
          foreach (string directory in Directory.GetDirectories(dir))
          {
              subDir.path = directory;
              subDir.subDirectories = new List&lt;SubDirectory&gt;();
              subDir.subDirectories = getSubDir(directory);
              dataList.Add(subDir);
          }
          return dataList;
      }
  </code></pre>
  
  <p>Later, i plan to release this program open source on GitHub. When i do, i will post the link here.</p>