title: Write all TreeView nodes to text file
body: <p>I'm trying to save all <code>TreeNodes</code> with <code>Text="Group"</code> from my <code>TreeView</code> to a file. I'm able to print all <code>TreeNodes</code> with <code>Text="Group"</code> but when I add a <code>StreamWriter</code> plus a file name , the result is a blank file. I am sure there are one or more <code>TreeNodes</code> which name is Group, so that couldn't be the problem. How can I fix my code?</p>
  
  <p>The result for the file can look like this for example</p>
  
  <blockquote>
    <p>Group 1</p>
    
    <p>Group 0</p>
  </blockquote>
  
  <p>EDIT:</p>
  
  <p>I found the fix I added <code>streamWriter.Close();</code> at the end of the <code>CreateSiveFile</code> method, now it works.</p>
  
  <p>Code</p>
  
  <pre><code>    public void CreateSaveFile(string fileName ,TreeView treeView)
      {
          StreamWriter streamWriter = new StreamWriter(fileName);
          // Print each node recursively.
          TreeNodeCollection nodes = treeView.Nodes;
          foreach (TreeNode n in nodes)
          {
              WriteRecursive(streamWriter, n);
          }
      }
  
      public void WriteRecursive(StreamWriter writer, TreeNode treeNode)
      {
          if (treeNode.Text == "Group")
          {
              writer.WriteLine(String.Format("{0} {1}", treeNode.Text, treeNode.Nodes.Count));
              // Print each node recursively.
              foreach (TreeNode tn in treeNode.Nodes)
              {
                  WriteRecursive(writer,tn);
              }
          }
      }
  </code></pre>