在上个项目开发中遇到这样的需求,取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。下面小编给大家分享c# 遍历文件夹子目录下所有图片及遍历文件夹下的文件,一起看看吧
要求:取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。
服务端代码:
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 } }
登录后复制
前端代码:
$(function(){ var images = ; var list = []; list.push(""); list.push(""); list.push(""); list.push(""); $.each(images, function (a,b) { if((a+1)%2==0){ list.push(""); list.push("
图标"); list.push(" | 路径"); list.push(" | 图标"); list.push(" | 路径"); list.push(""); list.push(""); list.push(" |
"+" | "+b+""); list.push(""); } if((a+1)%2!=0){ list.push(" | ||
"+" | "+b+""); } }) list.push(""); list.push(""); list.push(" "); var images = list.join(""); $("#imgs").append(images); }) 登录后复制 效果图如下: 下面给大家介绍下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:"); 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#遍历文件夹子目录下所有图片及遍历文件夹下的文件代码分享的详细内容,更多请关注【创想鸟】其它相关文章! 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。 发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2444609.html 赞 (0)
详细介绍winformC#获得Mac地址,IP地址,子网掩码,默认网关的代码实例(图)
上一篇
2025年3月3日 14:38:10
php无法使用curl怎么办
下一篇
2025年2月24日 05:43:40
AD推荐 黄金广告位招租... 更多推荐 |