【问题标题】:List Subfolder contents of subfolder along with the files using C# and MVC4使用 C# 和 MVC4 列出子文件夹的子文件夹内容以及文件
【发布时间】:2016-01-05 19:48:12
【问题描述】:

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

在控制器端我写了以下代码

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

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

【问题讨论】:

  • 可以在SO 上找到许多不同的工作示例 在这里进行谷歌搜索是一个结果stackoverflow.com/questions/14305581/…
  • 将您的 ExplorerModel 对象放在 Session["ExplorerModel"] 中(因为它不是一个简单的类型,不要尝试 ViewData 等,而是使用会话)并使用 ExplorerModel myExplorerModel = 在您的视图中检索它Session["ExplorerModel"] 作为 ExplorerModel。
  • 我正在获取文件夹和子文件夹的完整列表,但我无法在视图中正确列出它
      @foreach (Model.dirModelList 中的 Models.DirModel dir) { foreach (IFileModel 文件在 Model.fileModelList)

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


【解决方案1】:

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

ASP.NET MVC 3 Treeview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多