C# 遍历文件夹子目录下所有图片及遍历文件夹下的文件

C# 遍历文件夹子目录下所有图片及遍历文件夹下的文件,第1张

概述要求:取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。

要求:取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。

服务端代码:

 public partial class VIEwIcon : System.Web.UI.Page {  JArray ja = new JArray(); //定义一个数组  public string info = string.Empty;   protected voID Page_Load(object sender,EventArgs e)  {   var path1 = System.AppDomain.CurrentDomain.BaseDirectory;//获取程序集目录   string path = Path.Combine(path1,"Image","menu");//Path.Combine 将3个字符串组合成路径   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 返回指定目录的文件路径 SearchOption.AllDirectorIEs 指定搜索当前目录及子目录   //遍历string 型 images数组   foreach (var i in images){    var str = i.Replace(path1,"");//获取相对路径    var path2 = str.Replace("\","/");将字符“\”转换为“/”    ja.Add(path2);   }   info = Newtonsoft.Json.JsonConvert.SerializeObject(ja);//序列化为String  } }

前端代码:

<script type="text/JavaScript">  $(function(){   var images = <%=info%>;  var List = [];  List.push("<table>");  List.push("<thead>");   List.push("<tr>");   List.push("<td>图标</td>");   List.push("<td>路径</td>");   List.push("<td>图标</td>");   List.push("<td>路径</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>

效果图如下:

下面给大家介绍下C# 遍历文件夹下所有子文件夹中的文件,得到文件名

假设a文件夹在F盘下,代码如下。将文件名输出到一个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();      //遍历文件夹      foreach (DirectoryInfo NextFolder in dirInfo)      {         // this.ListBox1.Items.Add(NextFolder.name);        fileInfo[] fileInfo = NextFolder.Getfiles();            foreach (fileInfo Nextfile in fileInfo) //遍历文件        this.ListBox2.Items.Add(Nextfile.name);       }    }  }}

以上所述是小编给大家介绍的C# 遍历文件夹及子目录下所有图片的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的C# 遍历文件夹子目录下所有图片及遍历文件夹下的文件全部内容,希望文章能够帮你解决C# 遍历文件夹子目录下所有图片及遍历文件夹下的文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1256523.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存