1

I am suppose to list Subfolder(if sub-folder has files and sub-folder it should list that too) & files for particular folder :

on Controller side I have written following code

 public static List<DirectoryInfo> GetSubdirectories(DirectoryInfo directory)
    {
        // Set up the result of the method.
        List<DirectoryInfo> result = new List<DirectoryInfo>();

        // Attempt to get a list of immediate child directories from the directory
        // that was passed in to the method.
        DirectoryInfo[] childDirectories;
        try
        {
            childDirectories = directory.GetDirectories();
        }
        catch (UnauthorizedAccessException uae)
        {
            // If the permissions do not authorise access to the contents of the
            // directory then return an empty list.
            return result;
        }

        // Loop over all the child directories to get their contents.
        foreach (DirectoryInfo childDirectory in childDirectories)
        {
            // Add the child directory to the result list
            result.Add(childDirectory);

            // Get any children of the current child directory
            List<DirectoryInfo> grandchildDirectories = GetSubdirectories(childDirectory);

            // Add the child's children (the grandchildren) to the result list.
            result.AddRange(grandchildDirectories);
        }

        // return the full list of all subdirectories of the one passed in.
        return result;
    }

public ActionResult GetDocumentList(string sFolderName, string sFolderPath)
    {

        if (System.IO.File.Exists(sFolderPath))
        {
            return base.File(sFolderPath, "application/octet-stream");
        }
        else if (System.IO.Directory.Exists(sFolderPath))
        {

            List<FileModel> fileListModel = new List<FileModel>();

            List<DirModel> dirListModel = new List<DirModel>();

            IEnumerable<string> dirList = Directory.EnumerateDirectories(sFolderPath);

            foreach (string dir in dirList)
            {
                DirectoryInfo d = new DirectoryInfo(dir);

                DirModel dirModel = new DirModel();

                dirModel.DirName = Path.GetFileName(dir);
                dirModel.ParentName = d.Parent.Name.ToString();

                dirListModel.Add(dirModel);

               List<DirectoryInfo> s =  GetSubdirectories(d);
               foreach (DirectoryInfo d1 in s)
               {
                   DirModel dirModel1 = new DirModel();

                   dirModel1.DirName = d1.Name;
                   dirModel1.ParentName = d1.Parent.Name.ToString();

                  dirListModel.Add(dirModel1);
                  string str = d1.FullName.ToString();
                  IEnumerable<string> fileList = Directory.EnumerateFiles(str);

                  foreach (string file in fileList)
                  {
                      FileInfo f = new FileInfo(file);

                      FileModel fileModel = new FileModel();


                      {
                          fileModel.FileName = Path.GetFileName(file);
                          fileModel.DocumentPath = f.FullName;
                          fileModel.DirName = d1.Name.ToString();


                          fileListModel.Add(fileModel);
                      }
                  }
               }

            }

            IEnumerable<string> fileList1 = Directory.EnumerateFiles(sFolderPath);

            foreach (string file in fileList1)
            {
                FileInfo f = new FileInfo(file);

                FileModel fileModel = new FileModel();


                {
                    fileModel.FileName = Path.GetFileName(file);
                    fileModel.DocumentPath = f.FullName;
                    fileModel.DirName = "Main";
                    // fileModel.FileSizeText = (f.Length < 1024) ? f.Length.ToString() + " B" : f.Length / 1024 + " KB";

                    fileListModel.Add(fileModel);
                }
            }

            ExplorerModel explorerModel = new ExplorerModel(dirListModel, fileListModel);
           // db.Categories.Include(p => p.Products).ToList()
            return View("_ViewDocuments", explorerModel);
        }
        else
        {
            return Content(sFolderPath + " is not a valid file or directory.");
        }
    }

Model :

public class DirModel
{
    public string DirName { get; set; }
    public string ParentName { get; set; }

}
public class FileModel
{
    public string FileName { get; set; }
    public string DirName { get; set; }
    public string DocumentPath { get; set; }

}
public class ExplorerModel
{
    public List<DirModel> dirModelList;
    public List<FileModel> fileModelList;

    public ExplorerModel(List<DirModel> _dirModelList, List<FileModel> _fileModelList)
    {
        dirModelList = _dirModelList;
        fileModelList = _fileModelList;
    }
}

where I am stuck is at View how should I display Folder with its respective subfolder and files...any input will be great help..Thanks

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • lots of different working examples can be found here on `SO` doing a google search here is one result http://stackoverflow.com/questions/14305581/method-to-get-all-files-within-folder-and-subfolders-that-will-return-a-list – MethodMan Oct 08 '15 at 09:25
  • Put your ExplorerModel object in the Session["ExplorerModel"] (because its not a simple type don't try ViewData etc use the session instead) and retreive it in your view using something like ExplorerModel myExplorerModel = Session["ExplorerModel"] as ExplorerModel. – Paul Zahra Oct 08 '15 at 09:27
  • I am getting the complete List of folders and sub folder but I am not able to list it properly on views
      @foreach (Models.DirModel dir in Model.dirModelList) { foreach (IFileModel file in Model.fileModelList)
    – Sneha Deore Oct 08 '15 at 09:34

1 Answers1

0

You can use recursion methods to get all nested directories. Bind it to generic list and then you can easily manipulate at view level. To display at view level you can referto below link:

ASP.NET MVC 3 Treeview

Community
  • 1
  • 1
Rajesh KP
  • 17
  • 8