C# 遍历文件夹子目录下所有图片及遍历文件夹下的文件
作者:mrr 时间:2022-03-12 02:55:47
要求:取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。
服务端代码:
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,遍历,文件夹,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven+阿里云创建国内镜像的中央仓库(亲测可用)
2023-05-12 22:38:58
![](https://img.aspxhome.com/file/2023/3/86043_0s.jpg)
Java集合继承体系详解
2023-12-03 00:22:13
![](https://img.aspxhome.com/file/2023/3/76493_0s.jpg)
深入理解Java设计模式之备忘录模式
2023-09-20 06:16:43
![](https://img.aspxhome.com/file/2023/3/58433_0s.jpg)
springboot项目部署到宝塔的详细图文教程
2023-03-27 05:24:31
![](https://img.aspxhome.com/file/2023/0/86790_0s.png)
SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法
2023-05-16 12:53:02
Java @GlobalLock注解详细分析讲解
2023-06-03 03:55:53
![](https://img.aspxhome.com/file/2023/3/64543_0s.png)
java开发中使用IDEA活动模板快速增加注释的方法
2021-09-25 20:42:24
![](https://img.aspxhome.com/file/2023/9/86839_0s.png)
Android SDK Manager解决更新时的问题 :Failed to fetch URL...
2023-03-17 23:23:36
Springboot Vue实现单点登陆功能示例详解
2023-11-05 00:29:11
![](https://img.aspxhome.com/file/2023/7/58877_0s.png)
Spring框架应用的权限控制系统详解
2023-11-11 14:17:11
![](https://img.aspxhome.com/file/2023/2/59332_0s.png)
map实现按value升序排序
2022-10-23 23:13:49
spring boot使用自定义的线程池执行Async任务
2023-08-15 07:41:25
100行C#代码实现经典扫雷游戏
2023-12-05 16:33:30
![](https://img.aspxhome.com/file/2023/0/70400_0s.png)
java使用@Scheduled注解执行定时任务
2021-09-16 08:37:16
![](https://img.aspxhome.com/file/2023/6/65786_0s.jpg)
Java本地缓存工具之LoadingCache的使用详解
2023-06-24 11:28:11
java安全编码指南之:Mutability可变性详解
2023-11-11 06:30:24
![](https://img.aspxhome.com/file/2023/3/59073_0s.jpg)
mybatis中批量插入的两种方式(高效插入)
2023-09-16 22:21:07
Spring整合junit的配置过程图解
2022-12-18 16:37:48
![](https://img.aspxhome.com/file/2023/5/66975_0s.png)
C#实现自定义单选和复选按钮样式
2022-07-22 04:01:41
![](https://img.aspxhome.com/file/2023/2/87032_0s.png)
SpringBoot上传临时文件被删除引起报错的解决
2022-05-28 23:46:24
![](https://img.aspxhome.com/file/2023/6/74886_0s.jpg)