This article mainly for you in detail the C # Traverse folder and sub-directory of all the pictures of the relevant information, with a certain reference value, interested in small partners can refer to
requirements: take all the pictures under the specified directory, display the table's style and display the relative path of the picture.
Service-Side code:
Public partial class ViewIcon:System.Web.UI.Page { jarray ja = new Jarray ();//define an array public string info = Strin G.empty; protected void Page_Load (object sender, EventArgs e) { var path1 = system.appdomain.currentdomain.basedirectory;//Gets the assembly directory String path = Path.Combine (path1, "Image", "menu");// Path.Combine combines 3 strings into a path of var images = Directory.GetFiles (path, ".", Searchoption.alldirectories). Where (s = S.endswith (". png") | | s.endswith (". jpg") | | s.endswith (". gif")); Images = Directory.GetFiles (path, "*.png|*.jpg", searchoption.alldirectories); Directory.GetFiles returns the file path of the specified directory searchoption.alldirectories specifies the search current directory and subdirectories //Traversal string type images array foreach ( var i in images) { var str = i.replace (path1, "");//Get relative path var path2 = str. Replace ("\ \", "/"); convert the character "\ \" to "/" ja. ADD (path2); } info = Newtonsoft.Json.JsonConvert.SerializeObject (JA);//serialized to String }}
Front-End Code:
<script type= "Text/javascript" > $ (function () {var images = <%=info%>; var list = []; List.push ("<table>"); List.push ("<thead>"); List.push ("<tr>"); List.push ("<td> icon </td>"); List.push ("<td> path </td>"); List.push ("<td> icon </td>"); List.push ("<td> path </td>"); List.push ("</tr>"); List.push ("</thead>"); List.push ("<tbody>"); $.each (images, function (A, b) {if ((a+1)%2==0) {List.push ("<td>" + "</td> "); List.push ("<td>" +b+ "</td>"); List.push ("</tr>"); } if ((a+1)%2!=0) {List.push ("<tr>"); List.push ("<td>" + "</td> "); List.push ("<td>" +b+ "</td>"); }}) List.push ("</tbody>"); List.push ("</table>"); List.push ("<br>"); var images = List.join (""); $ ("#imgs"). Append (imagES); }) </script>
As follows: