


C# traverse all pictures in subdirectories of a folder and traverse files in a folder code sharing
在上个项目开发中遇到这样的需求,取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。下面小编给大家分享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); } } } }
The above is the detailed content of C# traverse all pictures in subdirectories of a folder and traverse files in a folder code sharing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Web Services in C#. Here we discuss an introduction to Web Services in C# with technology use, limitation, and examples.
