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);}}}