简体   繁体   中英

ASP.Net core 1 list files in given directory

我需要列出给定文件夹中存在的所有文件,将 C# 用于 ASP.Net 核心 1。类似早期版本中的System.IO.Directory.GetFiles()类的东西。

you can do something like this:

foreach (string file in Directory.EnumerateFiles(
            pathToFolder, 
            "*" , 
            SearchOption.AllDirectories) 
            )
        {
            // do something

        }

note that I'm recursing child directories too which may or may not be what you want

in asp.net core to list or search files you can use this way:

for example consider we want to find latest update file in this directory:

public IActionResult Get(IFileProvider fileProvider)
 {
      var files = fileProvider.GetDirectoryContents("wwwroot/updates");

      var latestFile =
                files
                .OrderByDescending(f => f.LastModified)
                .FirstOrDefault();

      return Ok(latestFile?.Name);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM