Link to home
Start Free TrialLog in
Avatar of Stacie
StacieFlag for United States of America

asked on

How to read file from a folder?

I have the following code and I'm trying to read all my image file from my upload folder. But I'm not sure what I did wrong with this code bellow.
I don't believe the path = Path.Combine logic is correct....

@{
    var folderName = "";
    var path = "";
    var files = new List<string>();
    
        folderName = Server.MapPath("/Uploads/").ToString();
        path = Path.Combine(Server.MapPath("/"), folderName);
  
        foreach(var file in Directory.GetFiles(path)){
           if(Path.GetFileName(file).Contains("_thumb_")){
               files.Add(Path.GetFileName(file));
       
        }
   }
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Gallery</title>
        
        <link rel="stylesheet" href="@Href("~/Content/tinycarousel.css")" type="text/css" />
        <link rel="stylesheet" href="@Href("~/Content/jquery.fancybox-1.3.4.css")" type="text/css" />
        <script type="text/javascript" src="@Href("~/Scripts/jquery-1.6.2.min.js")"></script>
        <script type="text/javascript" src="@Href("~/Scripts/jquery.tinycarousel.min.js")"></script>
        <script type="text/javascript" src="@Href("~/Scripts/jquery.fancybox-1.3.4.pack.js")"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('#slider-code').tinycarousel();
                $('.thumb').fancybox();
            });
        </script>	
    </head>
    <body>
        <h3>Gallery</h3>
        <div id="slider-code">
            <a class="buttons prev" href="#">left</a>
            <div class="viewport">
                <ul class="overview">
                    @foreach(var file in files){
                        <li>
                            <a class="thumb" href="Uploads/@folderName/@file.Replace("_thumb", "")">
                                <img src="Uploads/@folderName/@file" alt=""/>
                            </a>
                        </li>
                    }
                </ul>
            </div>
            <a class="buttons next" href="#">right</a>
        </div>
    </body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial