title: Loop through sub directories in directory
body: <p>I have a directory 'Folder' with many subdirectories inside this directory. Inside every subdirectory there are many images. I want to loop through subdirectories in the 'Folder' directory, then loop through all images in every directory to export the images out to Excel, with images from each subdirectory in one Excel worksheet.</p>
  
  <p>For e.g. if I have ten subdirectories, I should have one Excel workbook with ten Excel worksheets, then in each Excel worksheet there will be images from each subdirectory.</p>
  
  <p><strong>This is what I have tried but images only appeared on Worksheet1 instead of all the worksheets:</strong></p>
  
  <pre><code>   public void ExportToExcel()
          {
              //for export
              ExcelPackage objExcelPackage = new ExcelPackage();   //create new workbook
  
              string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/Folder"));
              int count = 0;
              int count1 = 0;
              int x = 25;
              int finalValue = 0;
  
              foreach (string subdir in filesindirectory)
              {                      
                  count++;
                  ExcelWorksheet ws = objExcelPackage.Workbook.Worksheets.Add("Worksheet" + count); //create new worksheet
  
              foreach (string img in Directory.GetFiles(subdir))
              {
                  count1++;
                  System.Web.UI.WebControls.Image TEST_IMAGE = new System.Web.UI.WebControls.Image();
                  System.Drawing.Image myImage = System.Drawing.Image.FromFile(img);
                  var pic = ws.Drawings.AddPicture(count1.ToString(), myImage);
                  // Row, RowoffsetPixel, Column, ColumnOffSetPixel
                  if (count1 &gt; 1)
                  {
                      pic.SetPosition(finalValue, 0, 2, 0);
                      finalValue += (x + 1); // Add 1 to have 1 row of empty row
                  }
                  else
                  {
                      pic.SetPosition(count1, 0, 2, 0);
                      finalValue = (count1 + x) + 1; // Add 1 to have 1 row of empty
                  }
              }
              }
  
              var filepath = new FileInfo(@"C:\Users\user\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx");
              objExcelPackage.SaveAs(filepath);
          }
  </code></pre>
  
  <p>How to loop through each sub directories in a directory, then loop through all images from each sub directory using C#?</p>