Get files in a folder

后端 未结 2 1770
无人共我
无人共我 2020-12-25 14:08

In my MVC application I have the following paths;

  • /content/images/full
  • /content/images/thumbs

How would I, in my c# controller, get a l

相关标签:
2条回答
  • 2020-12-25 14:15

    .NET 4.0 has got a more efficient method for this:

    Directory.EnumerateFiles(Server.MapPath("~/Content/images/thumbs"));
    

    You get an IEnumerable<string> on which you can iterate on the view:

    @model IEnumerable<string>
    <ul>
        @foreach (var fullPath in Model)
        {
            var fileName = Path.GetFileName(fullPath);
            <li>@fileName</li>
        }
    </ul>
    
    0 讨论(0)
  • 2020-12-25 14:24
    Directory.GetFiles("/content/images/thumbs")
    

    That will get all the files in a directory into a string array.

    0 讨论(0)
提交回复
热议问题