Home>

I encountered such a need in the last project development,Take all pictures under the specified directory,Show and display the relative path of the picture in a table format.Below we share with you all the pictures in the c#traversal folder subdirectory and the files in the traversal folder.Let's take a look

Requirement:Take all pictures under the specified directory,Show and display the relative path of the picture in a table format.

Server code:

public partial class viewicon:system.web.ui.page
 {
  jarray ja=new jarray ();//define an array
  public string info=string.empty;
  protected void page_load (object sender, eventargs e)
  {
   var path1=system.appdomain.currentdomain.basedirectory;//Get assembly directory
   string path=path.combine (path1, "image", "menu");//path.combine combines 3 strings into a path
   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 to search the current directory and subdirectories
   //Iterate through the string images array
   foreach (var i in images) {
    var str=i.replace (path1, "");//Get the relative path
    var path2=str.replace ("\\", "/");converts the character "\\" to "/"
    ja.add (path2);
   }
   info=newtonsoft.json.jsonconvert.serializeobject (ja);//Serialize 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>" + "&img width =" 50 "height =" 50 "src =" ../../"+ b +" "></td>");
    list.push ("<td>" + b + "</td>");
    list.push ("</tr>");
   }
   if ((a + 1)%2!=0) {
    list.push ("<tr>");
    list.push ("<td>" + "&img width =" 50 "height =" 50 "src =" ../../"+ b +" "></td>");
    list.push ("<td>" + b + "</td>");
   }
  })
  list.push ("</tbody>");
  list.push ("</table>");
  list.push ("<br>");
  var images=list.join ("");
  $("#imgs"). append (images);
 })
</script>

The following introduces the files in all subfolders of the c#traversal folder,Get file name

Assuming that the a folder is under the f drive, the code is as follows.Output filenames 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 the folder
      foreach (directoryinfo nextfolder in dirinfo)
      {
        //this.listbox1.items.add (nextfolder.name);
        fileinfo [] fileinfo=nextfolder.getfiles ();
        foreach (fileinfo nextfile in fileinfo) //traverse the file
        this.listbox2.items.add (nextfile.name);
      }
    }
  }
}
c
  • Previous C # salt + hash encryption
  • Next Windows nginx installation tutorial and simple practice