Implementing sample code sharing for all images in C # traversal folders and subdirectories

Source: Internet
Author: User
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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.