Loop through sub directories in directory

后端 未结 4 1921
醉话见心
醉话见心 2021-01-06 09:05

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 \

4条回答
  •  醉梦人生
    2021-01-06 10:01

    Janne Matikainen answer is correct but you need to know how to modify in your code...

    First change your code this line

     string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Folder"));
    

    to

     string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/Folder"));
    

    Secondly you need to search file in your sub folder path which is your

    foreach (string subdir in filesindirectory)
    

    subdir is your path for your directory.

    Just do back the same thing what you did to get the files

    foreach (string img in  Directory.GetFiles(subdir))
    

    After you finish the foreach subdirectory

    foreach (string img in  Directory.GetFiles(subdir)) 
    {
       // Your Code
    }
    // Reset the Count1
    count1 = 0;
    

    Reset it because you are increasing for dynamic row generating for each sheet.
    Then you at new sheet and you didn't reset it.
    It will continue the count as per previous sheet.

    To get the folder name you can easily get it by split.
    Before you create the worksheet you do as per below.

    string[] splitter = subdir.Split('\\');
    string folderName = splitter[splitter.Length - 1];
    

    Take NOTES if the folderName contain some symbol it might not able to set it to excel worksheet and also the name cannot be too long.
    Please ensure that you replace with supportable symbol for excel worksheet

提交回复
热议问题