title: Recursive Directory Structure Listing Taking To Long To Process
body: <p>I am using the code below start at a path (root) provided by a GET variable and recursively go into every sub folder and display it's contents as list items. The path I'm using has about 3800 files and 375 sub folders. I takes about 45 seconds to render the page, is there any way I can cut this time down as this is unacceptable for my users.</p>
  
  <pre><code>string output;
  protected void Page_Load(object sender, EventArgs e) {
      getDirectoryTree(Request.QueryString["path"]);
      itemWrapper.InnerHtml = output;
  }
  
  private void getDirectoryTree(string dirPath) {
      try {
          System.IO.DirectoryInfo rootDirectory = new System.IO.DirectoryInfo(dirPath);
          foreach (System.IO.DirectoryInfo subDirectory in rootDirectory.GetDirectories()) {
              output = output + "&lt;ul&gt;&lt;li&gt;&lt;a&gt;" + Regex.Replace(subDirectory.Name, "_", " ");
              if (subDirectory.GetFiles().Length != 0 || subDirectory.GetDirectories().Length != 0) {
                  output = output + " +&lt;/a&gt;";
              } else {
                  output = output + "&lt;/a&gt;";
              }
              getDirectoryTree(subDirectory.FullName);
              if (subDirectory.GetFiles().Length != 0) {
                  output = output + "&lt;ul&gt;";
                  foreach (System.IO.FileInfo file in subDirectory.GetFiles()) {
                      output = output + "&lt;li&gt;&lt;a href='" + file.FullName + "'&gt;" + file.Name + "&lt;/a&gt;&lt;/li&gt;";
                  }
                  output = output + "&lt;/ul&gt;";
              }
              output = output + "&lt;/li&gt;&lt;/ul&gt;";
          }
      } catch (System.UnauthorizedAccessException) {
          //This throws when we don't have access.
      }
  }
  </code></pre>