C # traverse the file folder directory under all the pictures and the file code sharing under the Traverse folders

Source: Internet
Author: User
In the last project development encountered such a requirement, take all the pictures under the specified directory, the form of the table to show and display the relative path of the picture. The following small series to share the C # traverse the folder directory of all the pictures and the files in the traversal folders, together to see it

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:

below to introduce you to the C # Traverse folder under all subfolders of the file, get the file name

Suppose the A folder is under the F disk, the code is as follows. Export the file name to a ListBox

Using system.data;using system.drawing;using system.linq;using system.text;using system.windows.forms;using System.io;namespace windowsformsapplication1{public  partial class Form1:form  {public    Form1 ()    {      InitializeComponent ();    }    private void Button2_Click (object sender, EventArgs e)    {      DirectoryInfo thefolder = new DirectoryInfo (@ "F:\a\") ;      directoryinfo[] Dirinfo = Thefolder.getdirectories ();      Traverse Folder      foreach (DirectoryInfo nextfolder in Dirinfo)      {         //THIS.LISTBOX1.ITEMS.ADD (nextfolder.name);        fileinfo[] FileInfo = Nextfolder.getfiles ();            foreach (FileInfo nextfile in FileInfo)//Traverse file        this.listBox2.Items.Add (nextfile.name);}}}  
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.