使用C#和MVC4列出子文件夹的子文件夹内容以及文件

时间:2015-10-08 09:18:36

标签: c# list file asp.net-mvc-4 views

我想列出子文件夹(如果子文件夹有文件和子文件夹,它也应该列出)&特定文件夹的文件:

在控制器端

我写了以下代码

 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.");
        }
    }

型号:

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;
    }
}

我被困在哪里查看我应该如何显示文件夹及其各自的子文件夹和文件......任何输入都会很有帮助。谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用递归方法获取所有嵌套目录。将它绑定到通用列表,然后您可以轻松地在视图级别进行操作。 要在视图级别显示,您可以在链接下方输入:

ASP.NET MVC 3 Treeview