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 } }
前端代码:
<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# 遍历文件夹及子目录下所有图片的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
您可能感兴趣的文章
- 01-10C#删除只读文件或文件夹(解决File.Delete无法删除文件)
- 01-10C#遍历文件夹后上传文件夹中所有文件错误案例分析
- 01-10C#编程实现获取文件夹中所有文件的文件名
- 01-10C#中用foreach语句遍历数组及将数组作为参数的用法
- 01-10C#程序中创建、复制、移动、删除文件或文件夹的示例
- 01-10Windows中使用C#为文件夹和文件编写密码锁的示例分享
- 01-10C#如何遍历Dictionary
- 01-10C#中哈希表(HashTable)用法实例详解(添加/移除/判断/遍历/排序等
- 01-10C#简单遍历指定文件夹中所有文件的方法
- 01-10C#遍历集合与移除元素的方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11ajax实现页面的局部加载
- 04-02jquery与jsp,用jquery
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05DEDE织梦data目录下的sessions文件夹有什